Support

Account

Home Forums General Issues Sort WP_Query by field first if it is checked, then this, then that

Helping

Sort WP_Query by field first if it is checked, then this, then that

  • I have a custom post type called, “portfolio”

    In CPT “portfolio”, I have custom fields (checkbox true or false), “include_case_study” and “include_review”, and a conditional field called, “include_video_review” if “include_review” is true.

    Okay, so, I am using WP_Query with custom $args to query all portfolio posts with “include_case_study” checked.

    I want to order those posts in a certain way, but just don’t know how to do it. I’d like the posts to be ordered like this:

    First Priority: posts with all three fields checked: include_case_study, include_review and include_video_review

    Then: posts with just these fields are checked: include_case_study, include_review

    Last: posts with only this field is checked: include_case_study

    This is how my $args looks like:

    
    $args = array(
      'post_type' => $portfolio,
      'meta_query'  => array(
        array(
          'key'   => 'include_case_study',
          'value'   => '1',
          'compare' => '==',
        ),
      ),
      'posts_per_page' => 7,
      'orderby' => 'date',
      'order' => 'DESC'
    )
    

    Thanks in advance for all your help!

  • Can you just create another custom field where you can update the value to be 1, 2, or 3 depending on how many values it has and then sort descending by that?

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

The topic ‘Sort WP_Query by field first if it is checked, then this, then that’ is closed to new replies.