query posts in wordpress

There is no term_id parameter. You need a tax_query.

$args = array(
    'post_type' => 'my_post',
    'posts_per_page' => 6,
    'tax_query' => array(
        array(
            'taxonomy' => 'yourtaxname',
            'field' => 'id',
            'terms' => 1
        )
    )
);
$query = new WP_Query( $args );

Notice that I’ve use new WP_Query and not query_posts. Don’t use query_posts.

Doing the above will create a new query. That is, it will request information from the data in addition to any information that has already been requested. That is fine, if that is what is what you need but if possible, it is better to alter the main query before any posts are retrieved. This reduces the load on the server which should translate into a quicker load time for the site.

To interrupt the main query use pre_get_posts. This would be the equivalent code for doing that.

function restrict_tax_wpse_100006($qry) {
  if (is_page('somepage_id_title_or_slug')) {
    $qry->set('post_type','my_post');
    $qry->set('posts_per_page',6);
    $qry->set('tax_query', 
        array( 
             array(
                  'taxonomy' => 'yourtaxname',
                  'field' => 'id',
                  'terms' => 1 // or array(1)
             ) 
        )
    );
  }
}

Leave a Comment