Skip Navigation

[Resolved] Having issues sorting by a custom field

This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 2 voices.

Last updated by Christian Cox 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#513650
Untitled-5.jpg
Untitled-4.jpg
Untitled-3.jpg
Untitled-1.jpg

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

#513796

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.

#513815

Here's my IP:
73.131.23.201

#514050

I actually don't have the ability to send private messages here, only to receive them. I will enable a private reply here again.

#514151

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.

#514160

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.

#514195

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.

#514216

Ah, ok, so where would I place that code, in the functions.php file or somewhere else?

moi06

#514484

Correct, this goes in functions.php.

#515079

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

#515092

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.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.