Support

Account

Home Forums Front-end Issues get all posts in custom post type by ACF field value

Solved

get all posts in custom post type by ACF field value

  • I have a custom post type with custom fields.
    one of the custom fields named program_id. i can`t understand how to get all of the posts in that CPT where the ACF field value is x, and only them. i read ACF guide but i always get all of the post.

    
    
    $student_query_args = [
        'post_type' => 'student_list',
        'post_status' => 'publish',
        'posts_per_page' => 100,
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'meta_query' => [
            'key' => 'program_id',
            'value' => 5317,
            'compare' => 'LIKE'
    
               ],
    
    ];
    
    $student_query = new WP_Query($student_query_args);
    
    if ($student_query->have_posts()) {
        while ($student_query->have_posts()) {
            $student_query->the_post();
            echo '<br>' . the_title() . '<hr/>';
        }
    }
    
  • OK I think it actually needs the ‘array’, like this:

    'meta_query' => array(
            'key' => 'program_id',
            'value' => '5317',
            'compare' => 'LIKE'
    
               ),

    You may or may not need the single-quotes around the value (‘5317’) it depends on how that custom field is setup (the type of field) and you could also try using ‘=’ in place of ‘LIKE’ but LIKE should work too.

  • meta_query needs to be a nested array

    either

    
    'meta_query' => array(
      array(
        'key' => 'program_id',
        'value' => '5317',
        'compare' => 'LIKE'
      )
    ),
    

    or

    
    'meta_query' => [
      [
        'key' => 'program_id',
        'value' => '5317',
        'compare' => 'LIKE'
      ]
    ],
    


    @trisham
    the second way is a shorthand that was added in PHP 5.4, similar to how arrays can be declared in JavaScript, which I prefer not to use. Mostly because my IDE does not understand it and shows a syntax error that prevents me from seeing real syntax errors. Besides, it only saves typing out “array” which I do instinctively and would save me what? Less than a second? I also find it a lot less clear when reviewing code.

  • @hube2 thank you for the explanation! I’m also so accustomed to typing ‘array’ that I don’t even think about it anymore….but it’s good to know the shortcut.

    In the OP’s case, then it looks like his query is written correctly *except* that my understanding is that “LIKE” is for strings, but the meta_value looks like a number, so perhaps adding the meta_type and changing the operator to ‘=’ should work?

    Like so:?

    'meta_query' => [
            'key' => 'program_id',
            'value' => 5317,
            'type' => 'numeric',
            'compare' => '='
    
               ],

    OR alternatively just do the query without using meta_query, like so:

    $student_query_args = [
        'post_type' => 'student_list',
        'post_status' => 'publish',
        'posts_per_page' => 100,
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'meta_key' => 'program_id',
        'meta_value' => '5317'
    ];
  • Thank you very much for your help.
    the meta_query needs to be a nested array.

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

The topic ‘get all posts in custom post type by ACF field value’ is closed to new replies.