0

I have a search query that I'm limiting to search within a custom post type. When the content is returned I have it sorting by meta value. The post meta is a person's position.

The function below works as expected, but I need to pull the posts and sort them in a certain order.

function kg_search_filter( $query ) {

    if( is_search() && ! is_admin() && $query->is_main_query() ) {

        $initial = ( !empty( $_GET['pro_search_initial'] ) ) ? $_GET['pro_search_initial'] : "";
        $expertise = ( !empty( $_GET['pro_search_expertise'] ) ) ? $_GET['pro_search_expertise'] : "";
        $office = ( !empty( $_GET['pro_search_office'] ) ) ? $_GET['pro_search_office'] : "";

        $query->set( 'post_type', array('professional') );

        if( $office ) {
            $query->set( 'tax-location', $office );
        }

        if ( $expertise ) {
            $query->set( 'tax-expertise', $expertise );
        }

        if ( $initial ) {
            $query->set( 'meta_query', array(
                        array(
                              'key' => 'professional_last_initial',
                              'value' => $initial,
                              'compare' => '='
                        )
                    ));
        }

        $query->set( 'posts_per_page', 10 );
        $query->set( 'meta_key', 'professional_position');
        $query->set( 'orderby', 'meta_value');
        $query->set( 'order', 'DESC');
    }
}
add_filter( 'pre_get_posts', 'kg_search_filter' );

What I'm after: First show all people with a president title, then all vice presidents, then the remaining.

I know I can use multiple loops as this solution suggests, but I'm not sure how to implement the second suggestion using pre_get_posts in reference to my query.

2
  • you're already ordering by another meta key? you can't use multiple loops if the results are paginated, you'll only sort a single page's results. it looks like you'll need to modify the SQL directly.
    – Milo
    Commented Dec 5, 2014 at 17:44
  • The meta key order can be removed. It was there for initial testing. I'll look into modifying direct SQL calls. Commented Dec 7, 2014 at 2:45

1 Answer 1

1

The concept of same meta field values being relative to each other isn't native to WordPress.

On top of my head you could create second meta field, which would express that relativeness as a number. So president is 1, vice president 2 and so on. This would be a pain to adjust if the hierarchy changes in future though.

3
  • Thanks for that. The hierarchy shouldn't change. The metabox for title is a simple text field. One option I'm considered is setting orderby to menu_order and letting the user control order via the admin post menu with drag and drop. Commented Dec 7, 2014 at 2:51
  • You don't have to make user control numerical values, you can do that with code depending on "friendly" value user sets. Also why is it a text field? If the title is freeform it would be hard to maintain hierarchy.
    – Rarst
    Commented Dec 7, 2014 at 8:35
  • The text field does present some difficulty. I wasn't given an specific list, so I opted to keep it freeform. Commented Dec 9, 2014 at 1:58

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.