Daniel
Daniel

Reputation: 79

How to display custom post types related to a specific taxonomy using WordPress

I am trying to display all custom post types of a specific taxonomy in WP.

Here is the code:

<?php

$args = array(
    'post_type' => 'team',
    'orderby'   => 'rand',
    'posts_per_page' => -1
);

$trit = new WP_Query($args);

while ($trit->have_posts()) : $trit->the_post();
    $post_id = get_the_ID();//POST ID
    $terms = get_the_terms($post->ID, 'tax_members' );
    ...
endwhile;

The script above displays all members of all taxonomies (tax_members). My goal is to display only the members of a specific category...for example: players.

How can I call the taxonomy players inside

$terms = get_the_terms($post->ID, 'tax_members' );

Upvotes: 0

Views: 51

Answers (2)

Niroj Adhikary
Niroj Adhikary

Reputation: 1835

<?php
            $queryArr = array(
            'post_type'        => 'team',
            'orderby'          => 'rand',
            'post_status'      => 'publish',
            'posts_per_page'   => -1,
            'tax_query' => array(
                        array(
                                'taxonomy' => 'tax_members',
                                'field'    => 'slug',
                                'terms'    => 'players',
                                'operator' => 'IN'
                            ),
                        ),
            );
            $trit = get_posts( $queryArr );
?>

Try this

Upvotes: 1

Richard Miles
Richard Miles

Reputation: 557

$args = array(
    'post_type' => 'team',
    'orderby'   => 'rand',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'tax_members',
            'terms'    => array('players'),
            )
        ) 
    );

Upvotes: 1

Related Questions