Support

Account

Home Forums General Issues Why isn't Meta query working with ACF?

Helping

Why isn't Meta query working with ACF?

  • I’m using a POST OBJECT acf field called “special_id” for a custom post type called ‘pencils’. If I remove the meta query from the args, I get the list of all Pencil posts it can find (drafts, specifically). When I add this meta_query, I get nothing back. Even though there are many draft posts with special_id “470”. I can use get_field(“special_id”, $pencil_type_id); and get the number 470 back.

    But why can’t I query for those with this (or any) matching id? Do I have a typo or something?

    $args = array(
    ‘post_type’ => ‘pencils’,
    ‘post_status’ => array(‘draft’),
    ‘numberposts’ => -1,
    ‘meta_query’ => array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => ‘special_id’,
    ‘value’ => 470,
    ‘compare’ => ‘=’,
    ),
    array(
    ‘key’ => ‘special_id’,
    ‘value’ => array(470),
    ‘compare’ => ‘=’,
    ),
    ),

    );
    $posts = get_posts($args);`

    get_post_meta( $some_pencil_id ) returns successfully:

    meta: Array
    (
     ...
        [special_id] => Array
            (
                [0] => 470
            )
    
      ...
    }
  • I’m using a POST OBJECT acf field called “special_id” for a custom post type called ‘pencils’. If I remove the meta query from the args, I get the list of all Pencil posts it can find (drafts, specifically). When I add this meta_query, I get nothing back. Even though there are many draft posts with special_id “470”. I can use get_field(“special_id”, $pencil_type_id); and get the number 470 back.

    But why can’t I query for those with this (or any) matching id? Do I have a typo or something?

    $args = array(
    ‘post_type’ => ‘pencils’,
    ‘post_status’ => array(‘draft’),
    ‘numberposts’ => -1,
    ‘meta_query’ => array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => ‘special_id’,
    ‘value’ => 470,
    ‘compare’ => ‘=’,
    ),
    array(
    ‘key’ => ‘special_id’,
    ‘value’ => array(470),
    ‘compare’ => ‘=’,
    ),
    ),

    );
    $posts = get_posts($args);`

    get_post_meta( $some_pencil_id ) returns successfully:

    meta: Array
    (
     ...
        [special_id] => Array
            (
                [0] => 470
            )
    
      ...
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.