I’m working on a custom taxonomy term page template where we want the items that are connected to the term sorted by a publication date (custom date field) – and if there are multiple items on the same day (formatted like YYYY-MM-DD) to then sort those by title, and finally sort by title if the custom field has not been filled out (older items).
So, I tried it hundred different ways with a WP_query and it does return most of the results as I want them – but in this case it’s only returning the items that have the meta_key of publication_date. All other items are being ignored and not displayed. I tried a meta_query using a relation of “or” and compared the publication_date as EXISTS and NOT EXISTS, but that returned 0 results for me.
Also, the site is running 3.5.2 still and they do not want to upgrade.
Here is my most recent query that gets me the posts that have the publication_date custom field displayed in the correct order:
$term = get_queried_object(); // find the term of the taxonomy page we are on
$wp_query = new WP_Query( array(
'post_type' => 'resource',
'tax_query' => array(
array(
'taxonomy' => 'resource_types',
'field' => 'slug',
'terms' => $term->name,
)),
'meta_key' => 'publication_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => '10',
));
I also tried using wpdb and running a SQL query, but I really am not sure how to accomplish what I want doing that. If someone could help me out that would be awesome!
Thanks in advance.
Thank you everyone for your help!
In the end the query below got me the results I desired – which was to show and sort the posts by a custom field of “publication_date” first – sorting by the date, and if there were multiple of the same date (say, 4 marked June 2013), it would sort those by title. Then, after it runs through all the posts that have the Publication Date filled in it will loop through again the remaining posts, alphabetically by title.
This gets me the results set in the same query, and keeps my pagination:
Few years later, the code posted by CSSGirl wasn’t working for me because there were some posts that didn’t have the meta key or the meta key was empty so this is what I had to do to have all the posts ordered by date and show the ones with a meta key value display first:
I think you’d need to do 2 separate loops. You can capture all the posts found in the first loop and exclude them from the secondary loop easily enough:
Then run your second loop.
Is there any reason you couldn’t enforce the publication_date meta key to exist for every post just with an empty value?
So in your
save_post
action you would add/update the meta key whether the$_POST
value is empty or not.You would have to run an update script to loop over your older posts and add the key with an empty value eg:
Run it by browsing to http://example.com/wp-admin/?update_old_posts
Then you can use the same query as you have. You may want to add an extra filter to enable you to order by different columns in different directions, it would make to sense to me to sort by date in descending order and title in ascending order.
I created a custom where clause. I tested it using
$wp_query->request
right before my main loop, I don’t really know SQL that well, but this seemed to get things working.Alternatively, you could set
compare
to'EXISTS'
and change the line in add_trending_where to$where .= " OR ($wpdb->postmeta.post_id IS NULL)";
. Then you’d only have to change the value of the key in one place. Again, echo$wp_query->request
and play around if you want understand this better or tweak it.EDIT: I just noticed this does not work if
meta_key
is set on the query. You could use$query->set('meta_key', NULL);
if you have to.EDIT 2: I got this working with the method above. For some reason it wasn’t at first (maybe meta_key was set… I don’t know).