Support

Account

Home Forums General Issues Query Multiple Conditions

Solved

Query Multiple Conditions

  • I’m trying to filter down a set of posts in a relationship field to “Published Only” + “ACF Field = Something”. So far, I’ve got the “published only” working:

    function bm_query_only_published_posts( $args, $field, $post_id ) {
    
        $args['post_status'] = array( 'publish' );
    
        return $args;
    }
    add_filter( 'acf/fields/post_object/query', 'bm_query_only_published_posts', 10, 3 );

    I need to further filter the query to also include only posts where an ACF toggle field “acf_toggle_field_123” is set to “yes”

    How/where would I expand the $args to include this?

  • Where is in your filter that you already have. How is by adding a meta query.

    Assuming you are talking about a true false field

    
    $args['meta_query'] = array(
      array(
        'key' => 'acf_toggle_field_123'
        'value' => 1 // true/false save 1/0 in the DB
      )
    );
    
  • Awesome, thanks John! That worked. (Though a comma was needed after the ‘key’ line)

    The Toggle was actually a radio button with “Yes” and “No”, but I get it with true/false. The final query arg ended up being:

    function bem_query_only_published_posts( $args, $field, $post_id ) {
    
        $args['post_status'] = array( 'publish' );
    
        $args['meta_query'] = array(
    	  array(
    		  'key' => 'acf_toggle_field_123',
    		  'value' => 'Yes' 
    	  )
    	);
    
        return $args;
    }
    
    add_filter( 'acf/fields/post_object/query', 'bem_query_only_published_posts', 10, 3 );
  • Thanks again John. My initial question was answered, but if you’re still willing…

    The results are sorted alpha by Title (default) I’d like to sort the results by yet another ACF which is a date/time field = ‘start_date_time’

    (These are a CPT called “Events” and each event has a start / end date & time. I’d like to sort the results in order of the date/time that each Event takes place)

    I don’t know where to put ‘orderby’ values, etc. Not a biggie, but gosh I wish I knew more PHP than novice level 🙂

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

You must be logged in to reply to this topic.