0

I have a custom post type called "data-base" which always need to be sorted by a custom field called "title_stand".

I have managed to apply the proper sorting to the archive-data-base.php file, however I'm having trouble applying that same sorting to the search results page.

Could anyone point me in the right direction to sort the search results page by alphabetical order from A to Z of the custom field "title_stand" value?

Thanks in advance!

***** UPDATE *****

Based on @PieterGoosen comment I have managed to get to this point:

// Sort search results by standarized title
function my_search_query( $query ) {
    // not an admin page and is the main query
    if ( !is_admin() && $query->is_main_query() ) {
        if ( is_search() ) {
            $args = array(
               'meta_key' => 'title_stand',
               'orderby' => 'meta_value',
               'order' => 'ASC'
            );
            $query->set( 'orderby', $args );
        }
    }
}
add_action( 'pre_get_posts', 'my_search_query' );

This seems to affect the search results order but not not in the way I was looking for and I cant seem to find the logic behind the new order...

2
  • pre_get_posts with the custom field parameters with ordering by meta_key Oct 4, 2015 at 13:28
  • @PieterGoosen Thanks for helping out! I have looked into pre_get_posts and custom field parameters but I'm still not able to get the result I'm looking for. Would you be able to point me to a more concrete example? Thanks! Oct 4, 2015 at 14:05

1 Answer 1

2

I have managed to apply the proper sorting to the archive-data-base.php file

Reading your question, I really do feel that you did not do your sorting correctly in archive-data-base.php as the same logic will be used for the search page as well. I do believe that you have used a custom query to correct the sorting, which you should never do. I'm not going to explain everything here as I have already done an extensive post on this here.

To correct the sorting in your archive-data-base.php and search page, you need to use pre_get_posts to alter the main query with the custom field parameters

You can try the following

add_action( 'pre_get_posts', function ( $q )
{
    if (    !is_admin() // Target only front end queries
         && $q->is_main_query() // Target the main query only
         && ( $q->is_search() || $q->is_post_type_archive( 'data-base' ) )
    ) {
        $q->set( 'meta_key', 'title_stand' );
        $q->set( 'order',    'ASC'         );
        $q->set( 'orderby',  'meta_value'  );
    }
});
4
  • Your code sorted everything exactly as it should! I wil revise the archive-data-base.php file too to follow this method. Cant thank you enough! :) Oct 4, 2015 at 14:57
  • My pleasure. Enjoy ;-) Oct 4, 2015 at 14:58
  • thanks for this answer, for me its not working if the value title_stand or whatever is not set. so we have to set default values for this to work?
    – RobBenz
    Apr 26, 2017 at 13:29
  • This works perfectly using ACF fields too. Just change title_stand to the advanced custom field name.
    – Elron
    Apr 28, 2020 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.