lowercase
lowercase

Reputation: 1220

Wordpress Advanced Custom fields loop with multiple meta_value

I am running a loop which requires content from multiple meta_values. I have a meta key called 'content_type'. Attached I have three meta_values for the key.

My code...

<?php $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
      $query_args = array(
      'post_type' => 'post',
      'meta_key' => 'content_type',
      'meta_value' => 'news',
      'posts_per_page' => 3,
      'paged' => $paged
      );
      $the_query = new WP_Query( $query_args );
?>

I need to show content from THREE meta_values. 'news', 'videos' and 'recipes'. My code currently pulls from only one - 'news'.

What is the best way to pull from all three?

Upvotes: 1

Views: 1410

Answers (1)

Yuxel Yuseinov
Yuxel Yuseinov

Reputation: 340

I suggest meta query -- https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters. In your case smth in this fashion:

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

$args = array(
    'post_type'  => 'post',
    'paged' => $paged
    'posts_per_page' => 3,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'content_type',
            'value'   => 'news',
            'compare' => '=',
        ),
        array(
            'key'     => 'content_type',
            'value'   => 'videos',
            'compare' => '=',
        ),
        array(
            'key'     => 'content_type',
            'value'   => 'recipes',
            'compare' => '=',
        ),
    ),
);
$query = new WP_Query( $args );

I am not sure how the custom field is stored in the database with ACF ( with or without "_" check it please )

Upvotes: 1

Related Questions