Search code examples
phpwordpresstaxonomy

Do wordpress loop with a specific term


How can I do the wordpress post loop, using a custom post type, to display only post from a certain category of a custom taxonomy?

$args = array(
    'post_type' => 'news',
    'taxonomy' => 'newscat',
    'field' => 'slug',
    'terms' => 'news',
    'posts_per_page' => 6,
);
$query = new WP_Query($args);
if($query -> have_posts()):while($query -> have_posts()):$query -> the_post();

Solution

  • Your arguments are incorrect.

    Try this.

    $args = array(
     'post_type' => 'news',
     'posts_per_page' => 6,
     'tax_query' => array(
        array(
         'taxonomy' => 'newscat',
         'field' => 'slug',
         'terms' => 'news'
      ),
     ),
    );
    $query = new WP_Query($args);
    if($query -> have_posts()):while($query -> have_posts()):$query -> the_post();