1

I have a custom post type with custom fields(advanced custom fields plugin). 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,
  'order' => 'DESC',
  'meta_query' => [
      'key' => 'program_id',
      'value' => 5317,
  ],

];

$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/>';
  }
}
  • try adding 'compare' => 'LIKE', attribute in meta_query bellow value. – Chetan Vaghela Aug 20 '19 at 10:54
  • i did. and its not working. – Doron Davidowitz Aug 20 '19 at 10:59
0

Meta Queries are nested arrays. See the WP_Query section on meta queries.

Option 1

Use meta_key and meta_value directly in the query arguments, not as a meta query.

$student_query_args = [
  'post_type'       => 'student_list',
  'post_status'     => 'publish',
  'posts_per_page'  => 100,
  'order'           => 'DESC',
  'meta_key'        => 'program_id',
  'meta_value'      => 5317,
];

Option 2

The meta query approach. If you're adding multiple you would need multiple arrays. The default relation is AND but we'll supply it for clarity:

$student_query_args = [
  'post_type'       => 'student_list',
  'post_status'     => 'publish',
  'posts_per_page'  => 100,
  'order'           => 'DESC',
  'meta_query'      => [
      'relation' => 'AND',
      [
          'key'     => 'program_id',
          'value'   => 5317,
      ],
  ],
];
  • you are right. i haven't nested the met_query – Doron Davidowitz Aug 21 '19 at 6:59
  • @DoronDavidowitz If the above solved your issue you can upvote and click the checkmark to show future readers that your issue has been addressed. Glad to help! – Howdy_McGee Aug 21 '19 at 13:37
  • 1
    i did it all ready, but "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Doron Davidowitz Aug 22 '19 at 8:39
0

as Howdy_McGee notice. the meta_query needs to be a nested array and to add 'LIKE' as comparison. the correct answer is

$student_query_args = [
  'post_type' => 'student_list',
  'post_status' => 'publish',
  'posts_per_page' => 100,
  'order' => 'DESC',
  'meta_query' => [
      'key' => 'program_id',
      'value' => 5317,
      'type' => 'numeric',
      '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/>';
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.