Natural sort Wordpress post titles (alphabetically and numerically)? - php

Is there any possible way to sort a new Wordpress post query by the title, but numerically instead of alphabetically?
I have some titles that have a lot of the same name alphabetically, then have a number afterwards, so of course for example Wordpress is putting title12 ahead of title1.
$args = array(
'orderby'=> 'title',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
I know we have this functionality to sort titles in ascending order, but it does not sort titles like that:
Title 1
Title 2
Please let me know if we any work around using WP query ?
Thanks for your help in advance :)

Try adding this immediately after your code above:
usort($loop->posts, function($a,$b) {
return strnatcmp($a->title, $b->post_title);
});

Related

Get posts with unique titles in WP Query

I have a query that takes posts of different types with the same title. Instead, I want to pull only posts with unique titles.
My query so far is as follows:
$args = array(
'post_type' => ['report', 'case-study', 'event'],
'posts_per_page' => $posts_to_get,
'post__not_in' => [$post->ID]
);
$query = new WP_Query($args);
However, this does not filter only unique titles.
I can get a list of unique titles using
$uniques = array_unique($titles); but that does not get me the actual posts. I wanted to know if there was a query method along the lines of 'post__title_not_in => $uniques;
I tried searching everywhere for this, but I was not able to find anything that worked. Would greatly appreciate any help.

Getting a count of Top level Categories WordPress

i am trying to get a count of top level categories , i am using wordpress 2016 theme , its a static front page and i am trying to retrieve the total count of categories and then dynamically create table with those categories
i tried to search with WP_Query and wp_list_categories it does list names but not the total count , i tried to use count function in php but output is always 0(zero) even though i have few categories / posts etc already created to test this functionality.
please advise or assist with the code
Thanks
Try Below Code :
<?php
$args = array(
'parent' => 0,
'hide_empty' => 0
);
$categories = get_categories( $args );
echo count( $categories );
?>
You can also wrap it in a function to re-use it else where in your site.
Hope this will help you!

How to order multidimensional arrays PHP

I've been stuck for hours in trying to order get_pages() object from wordpress.
I need to order the pages by menu_order (get_pages->menu_order);
I've been trying several PHP functions but none worked in such purpose.
PHP
foreach(get_pages(pll_current_language()) as $page) {
// Somewhere here each page should go up/down in order to be DESC/ASC
$template = get_post_meta( $page->ID, '_wp_page_template', true );
include_once($template);
}
My question is, How can I order this object by one of its values,
example:
I want to order get_pages() by get_pages()->menu_order
Any help will be appreciated.
just use the $args options array and set the sort_order, sort_column on whatever you need, check the docs https://codex.wordpress.org/Function_Reference/get_pages
$args = [
'sort_order' => 'asc',
'sort_column' => 'menu_order'
];
$pages = get_pages($args);
You probably want to use the usort function. You provide it with a comparison function (in your case a function that compares $a->menu_order to $b->menu_order) and it will then use it to sort the elements in your array.

Show all posts under a same custom field

So, I have a custom field called "geo_location".
And under that field, there are total 5 different values (1 to 5).
Now, there are total 50 posts that have one of the five values.
For example, post#1 has geo_location="1" and post#2 has geo_location="3" etc.
Is there a way to show all the posts under the same 'geo_location' value?
So for example, I want to show all the posts under the same geo_location="1".
how can I achieve this?
Thank you.
You need to use WP_Query to create a database query using your custom meta-data.
This is done like this:
$args = array(
'meta_key' => 'geo_location',
'meta_value' => '1'
);
$query = new WP_Query( $args );
Then you can output the result using the loop in the usual way.

How to fix this WordPress function so that it doesn't return a 404 page?

I have the following function that I've added to my functions.php file in WordPress. The idea is that it gathers all of the titles of 'fsmodel' posts (a custom post type that I've created). It then returns these as an array, which I then use to populate a select tag in the custom meta fields for a second custom post type.
Basically, 'fsmodel' will have posts with a boat model, and the 'fsboat' post type will have a drop-down with the names of each of the models to select from.
Now, this appears to works fine in the Dashboard - the drop-down is populated as expected. When I save, however, the post doesn't show up in the Edit list. Also on the website, all pages output as the 404 error page when this function is active.
I'm certain that the problem lies within the following code - does anyone have any idea what I might have done wrong?
function fs_model_array() {
$models_array = array();
$loop = new WP_Query(array(
'post_type' => 'fsmodel',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish'
));
while ( $loop->have_posts() ) : $loop->the_post();
$models_array[] = get_the_title();
endwhile;
return $models_array;
};
OK, I've come up with a solution (I hope - it's holding up for now).
Instead of creating a loop, I've just used the $wpdb->get_results to search the database for the column with a WHERE filter for the custom post type.
Then run an array builder:
$models_array = array();
$model_db = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_type='fsmodel' AND post_status = 'publish'");
foreach ($model_db as $model_db) {
$models_array[] = $model_db->post_title;
}
Thanks again for your time, hsatterwhite! :-)
I think you might find that adding wp_reset_query() to the end of your function will solve your problems :)
I like your solution, but I'd be inclined to say that you need to call the global variable of $post whenever you use the loop like this in a function, as it assigns it to that variable.
function fs_model_array(){
global $post;
$models_array = array();
$loop = new WP_Query(array(
...

Categories