Hi Christian,
Hope all is well. I'm having some issues with ordering by one of my custom fields. When I order by Post Date, it works, although it doesn't order my list in order of Post Date. So I tried ordering by a custom field, Publication Year, and that just doesn't work at all. I've added some images as well.
moi06
Hi, if you want to order by the Year custom field, you should probably ensure that you are comparing "as a number" instead of "as a string" - see Untitled-4.jpg
However, I'm not sure why this would be necessary. Ordering by Post Date should work for you. What's going on there? If you'd like to share your wp-admin access credentials I can take a deeper look.
Here's my IP:
73.131.23.201
I actually don't have the ability to send private messages here, only to receive them. I will enable a private reply here again.
Okay I see what's going on here. In short, you are ordering your results by post date, but the post date is that of the intermediary post type rather than the publication post type. So it appears that the sorting order isn't working correctly. In fact, this is a limitation of WordPress queries. Views uses the WP_Query class to query posts:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
The sort order parameters that can be used all apply to the post type you choose in the View's Content Selection area.
However there is a workaround using the wpv_filter_query_post_process Views filter to order the search results. You can find more information about that here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
Another forum post describes the approach in detail:
https://toolset.com/forums/topic/sort-loop-output-of-children-of-the-post-set-by-parent-view/
Let me know if you need assistance setting up this filter and implementing it on your site.
So basically the sort filter is useless. How can I sort based on the publications and not the intermediary using the sort filter? What's the purpose of having this suite of plugins if you always need to code up to make something work. Do you have an example.
You're right, in this case the sort filter GUI won't help you. I have an example for you:
// sort results of child post type A by parent post type B post date
add_filter( 'wpv_filter_query_post_process', 'sort_query_func', 10, 3 );
function sort_query_func( $query, $view_settings, $view_id ) {
if ( !empty( $query->posts ) && $view_id == 5045) {
usort($query->posts, "post_date_cmp");
}
return $query;
}
function post_date_cmp($a, $b)
{
$publications_a_id = get_post_meta($a->ID, '_wpcf_belongs_publications_id', true);
$publications_a_date = get_the_date('U', $publications_a_id);
$publications_b_id = get_post_meta($b->ID, '_wpcf_belongs_publications_id', true);
$publications_b_date = get_the_date('U', $publications_b_id);
$res = 0;
if ( $publications_a_date < $publications_b_date) {
$res = 1;
}
return $res;
}
This assumes the view ID is 5045 and the post type slug is "publications" - I believe these should be correct for your case.
Ah, ok, so where would I place that code, in the functions.php file or somewhere else?
moi06
Correct, this goes in functions.php.
Hey Christian,
So that code works. However, its sorting based on the year of the post date. I'm trying to sort based on a custom post field, Publication Year. The slug for the Publication Year field is publication-year. Can you show me what that code would look like to work properly?
moi06
Assuming the year is stored as a string like "2017" or "2016" for each Publication, you can make the following modification (lines 11-14 of the previous code post):
$publications_a_id = get_post_meta($a->ID, '_wpcf_belongs_publications_id', true);
$publications_a_date = get_post_meta($publications_a_id, 'wpcf-publication-year');
$publications_b_id = get_post_meta($b->ID, '_wpcf_belongs_publications_id', true);
$publications_b_date = get_post_meta($publications_b_id, 'wpcf-publication-year');
Instead of comparing the post date, now we are comparing the custom field value for publication-year.