Home Forums ACF PRO Show only posts with acf field set to false and other args


Show only posts with acf field set to false and other args

  • Hi, any idea why this code doesn’t work ?
    Once I added meta_query, my relationship field show empty list.
    Spent an hour and i give up 😉

    btw. setting $switch_value to true, shows only 2 posts, but null doesn’t show full list.
    btw2, ‘compare’ => ‘!=’ and $switch_value to true doesn’t show full list aswell.

    // Show 2000 posts pre ajax call, change sort
    add_filter('acf/fields/relationship/query/name=author_sort_list', 'my_acf_fields_relationship_query', 10, 3);
    function my_acf_fields_relationship_query( $args, $field, $post_id ) {
        // Define the ACF field name
        $switch_name = 'program_onlyschedule_switch';
        // Define the value to exclude
        $switch_value = null;
        // Create the meta query
        $args['meta_query'] = array(
                'key'     => $switch_name,
                'value'   => $switch_value,
                'compare' => '='
        $args['posts_per_page'] = 2000;
        $args['orderby'] = 'date';
        $args['order'] = 'ASC';
        return $args;
  • You likely also need to check to see if the meta key doesn’t exist at all with a NOT EXISTS in the compare. So you’re doing an ‘OR’ relation meta query looking for either a null state or that the key does not exist at all.

    If you have a true/false field and you save a post in the false state, it doesn’t create the key with an initial false state.

  • ACF fields always have a value and that value is never NULL. You cannot use NOT EXISTS. For an empty value you need to use and empty string. What you would use for some fields might change based on the field type.

  • @hube2 , We are talking about true / false field. Empty value doesn’t work too.

  • A true false field has a value of 0 (zero) for false and a value of 1 for true.

  • If you added a true/false field to a content type and its default state is false, you would want to also check for NOT EXISTS, though, to return the full set, as ACF won’t autopopulate the false state for all posts, no? So usually if I’m working with a true/false field, my meta query is looking both for posts with the 0 value (false) in the key or posts where the key doesn’t exist at all.

  • No, ACF will not retroactively update all posts, so you would have to check for not exists for whatever you set the default value to.

  • thx guys for your input

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

You must be logged in to reply to this topic.