Support

Account

Home Forums Backend Issues (wp-admin) Filtering on custom field and sorting on another custom field

Solved

Filtering on custom field and sorting on another custom field

  • This support topic shows how to Order by a custom field:
    https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/

    This support topic shows how to filter on 1 or more custom fields:
    https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    But how do I do both?

    Part one – I want to filter on the value from the radio button field ‘member_category’ and limit it to the value ‘Leadership’. THIS WORKS

    <?php
      $args = array ( 
        'numberposts'	=> -1,
        'post_type'		=> 'member',
        'post_status' => 'publish',
        'meta_key'		=> 'member_category',
        'meta_value'	=> 'Leadership',
        //'meta_key'    => 'name_last_name',
        //'orderby'			=> 'meta_value',
        'order'       => 'ASC'
      ); 
    ?>

    I want to sort on the sub field ‘last_name’ which is is a group container of ‘name’. – THIS WORKS

    <?php
      $args = array ( 
        'numberposts'	=> -1,
        'post_type'		=> 'member',
        'post_status' => 'publish',
        //'meta_key'		=> 'member_category',
        //'meta_value'	=> 'Leadership',
        'meta_key'    => 'name_last_name',
        'orderby'			=> 'meta_value',
        'order'       => 'ASC'
      ); 
    ?>

    It tried combining them, but this DOES NOT WORK.

    <?php
      $args = array ( 
        'numberposts'	=> -1,
        'post_type'		=> 'member',
        'post_status' => 'publish',
        //'meta_key'		=> 'member_category',
        //'meta_value'	=> 'Leadership',
        //'meta_key'    => 'name_last_name',
        //'orderby'			=> 'meta_value',
        //'order'       => 'ASC',
        'meta_query' => array (
                                array (
                                  'meta_key' => 'name_last_name',
                                  'orderby' => 'meta_value',
                                  'order'       => 'ASC',
                                ),
                                array (
                                  'meta_key' => 'member_category',
                                  'meta_value' => 'Leadership'
                                )
                              )
        
      );
    ?>

    Any clues?

  • HI @ger-ang

    I think you can do something like:

    <?php
    $args = array ( 
        'numberposts'	=> -1,
        'post_type'		=> 'member',
        'post_status' => 'publish',	
        'meta_query' => array(
            'relation' => 'AND',
            'leadership_clause' => array(
                'key' => 'member_category',
                'value' => 'Leadership',
            ),
            'name_clause' => array(
                'key' => 'name_last_name',
                'compare' => 'EXISTS',
            ), 
        ),
        'orderby' => array( 
            'leadership_clause' => 'ASC',
            'name_clause' => 'ASC',
        ),
    );

    Code is untested!

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

You must be logged in to reply to this topic.