Support Forums

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Sort Results by Meta Value and THEN by Title?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Lars Faye
    #146171

    Hi there,

    We have a use case where we want to feature certain Faculty members and have them appear before the rest of the members, no matter what search results are being displayed.

    Right now, if I set the Default Order to a Meta Value, it returns those posts with said meta value (which is a boolean), but it ignores any posts that do not have that meta value checked. What we’d like to have happen is have the query check for any posts with that meta value, show those first, and THEN show all the rest of the posts that do not, sorted alphabetically.

    Is this possible with S&F Pro?

    Trevor Moderator
    #146222

    At this time, this is the expected behavior (what you are seeing), and with the current version of Search & Filter it is not a feature to do as you ask. There has to be a value in the meta field if you use it in a sort or slider field, otherwise the post with the missing data will be omitted from the results. More options may come with V3 next year (2018).

    Lars Faye
    #146347

    Well, I got good news…this is actually totally possible using your Query filter/hook! I ran a relational “OR” meta query on the field and it seems to be performing as I desire (places all posts with the meta value at the front, sorts alphabetically, then shows the rest of the posts without the meta value also in alphabetical order).

         $query_args          = array(
            'post_type' => 'faculty',
            'posts_per_page' => -1,
            'meta_query'  => array(
                'relation' => 'OR',
                array(
                    'key'     => 'featured_faculty',
                    'compare' => 'EXISTS',
                ),
                array(
                    array(
                        'key'   => 'featured_faculty',
                        'compare' => 'NOT EXISTS',
                    ),
                ),
            ),
            'orderby' => array( 'meta_value' => 'DESC', 'title' => 'ASC' ),
        );

    You can check it out here:

    https://cogswell.edu/about-us/faculty-new/

    Trevor Moderator
    #146437

    Cool. Thanks for sharing.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Sort Results by Meta Value and THEN by Title?’ is closed to new replies.