Request posts from two post formats per category

I was reading WP_Query Codex looking for a way to iterate over all posts that are in post 'video' OR 'image' format within a given category.

If that's not enough, that category is set to a variable $catslug

(I need this to be the case).

I just found ways to iterate over one of the following

  • image OR video

  • image And category

  • video AND category,

but what i need is more complicated, something like this:

post-format-image

AND $catslug

) OR ( post-format-video

AND $catslug

)

Can it be done tax_query

within tax_query

?

Something like that:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => array($catslug)
            ),
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-image' )
            )
        ),
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => array($catslug)
            ),
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video' )
            )
        )
    ) 
); 
$query = new WP_Query( $args );

      

Does anyone know a workaround or a hack for this? Perhaps I am just wrong.

+3


source to share


1 answer


This is a really good question. The simple answer here is that you cannot use multiple tax_query

.

I had to quickly check the following scenario before I left for work. Just for fun, I went and tried using the category options with help tax_query

, but that gave me posts from the category I wanted and posts that also apply to both post formats

I came up with a possible solution, unfortunately I cannot test this right now.

Here is my line though here:

As you need random results, I would suggest adding your message formats to an array



$post_format_array = array( 'post-format-video', 'post-format-image' );

      

Now you are going to use shuffle

to shuffle an array, and then take the first entry from that array, which will either video, and use it in tax_query

.

shuffle( $post_format_array );    

      

This way you will receive messages that are in your desired category and in video or image format.

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
    'relation' => 'AND',
      array(
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => $catslug,
      ),
      array(
        'taxonomy' => 'post_format',
        'field'    => 'slug',
        'terms'    => $post_format_array[0]
      ),
    ),
);
$query = new WP_Query( $args );

      

+1


source







All Articles