Looking for WP_Query help (2 posts)

  1. sparks2326
    Posted 1 year ago #

    I'm trying to add a condition to a query string and am striking out. I've read through the support docs several times and am either just missing the answer or am too inexperienced to see it.

    This is my current string:

    <?php $recent = new WP_Query( 'cat=7,316&posts_per_page=10' ); ?>

    And what I want to do is also include posts from a certain tag, so I tried adding


    But I realize this is probably saying "give me all posts with a cat id of 7 AND a tag id of 7, and what I want is cat 7 OR tag 7.

    I think one easy answer here is to just use create and include a third category, but I am trying to include these post headlines in a specific spot on the homepage without them showing up on a separate category archive page.

  2. vtxyzzy
    Posted 1 year ago #

    You need to use a 'tax_query' as described here: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    Your code would look something like this:

    $args = array(
       'post_type' => 'post',
       'posts_per_page' => 10,
       'tax_query' => array(
          'relation' => 'OR',
             'taxonomy' => 'category',
             'field' => 'id',
             'terms' => array( 7, 316 )
             'taxonomy' => 'post_tag',
             'field' => 'id',
             'terms' => 7
    $recent = new WP_Query($args);

Topic Closed

This topic has been closed to new replies.

About this Topic