WordPress.org

Forums

Looking for WP_Query help (2 posts)

  1. sparks2326
    Member
    Posted 9 months 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

    &tag_id=7

    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
    Member
    Posted 8 months 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:

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

Reply

You must log in to post.

About this Topic