$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Listing all custom posts having a specific taxonomy whatever the terms|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Listing all custom posts having a specific taxonomy whatever the terms

matteradmin9PV0评论

I'd like to get a list of all the custom posts that belong to a specific taxonomy.

I've tried many things including this code, but I get a list of all the posts in the 'members' cpt, and not just posts associated to the 'producers' taxonomy. How can I get it work ?

<?php
$args = array(
        'post_type' => 'members',
         'posts_per_page' => -1,
         'tax_query' => array(
        'taxonomy' => 'producers'
),
);
    $the_query = new WP_Query($args);
    while ( $the_query->have_posts() ) : $the_query->the_post();
        ?>
            <li class="producers" id="post-<?php the_ID(); ?>">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
            <?php
    endwhile;
 ?>

EDIT 2018-10-31

I finally made it through native WP functions and a custom query. I also needed the pagination functionality so I built it this way.

    $termArray = [];
    $theTerms = get_terms('producers');
    foreach ($theTerms as $singleTerm) {
        $theSlug = $singleTerm->slug;
        array_push($termArray,$theSlug);
    }
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $loop = new WP_Query( array( 'post_type' => 'members', 'orderby' => 'rand', 'posts_per_page' => 5, 'paged' => $paged, 'tax_query' => array(
        array(
            'taxonomy' => 'producers',
            'terms'    => $termArray,
        )
    )));
    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post();
        $terms = get_the_terms( $post->ID, 'producers');
        if ($terms) {
            /// Here is the code for posts display
        }
    endwhile;
    endif;
    wp_reset_postdata();


// Pagination
    $big = 99999;
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $loop->max_num_pages
    ));

I'd like to get a list of all the custom posts that belong to a specific taxonomy.

I've tried many things including this code, but I get a list of all the posts in the 'members' cpt, and not just posts associated to the 'producers' taxonomy. How can I get it work ?

<?php
$args = array(
        'post_type' => 'members',
         'posts_per_page' => -1,
         'tax_query' => array(
        'taxonomy' => 'producers'
),
);
    $the_query = new WP_Query($args);
    while ( $the_query->have_posts() ) : $the_query->the_post();
        ?>
            <li class="producers" id="post-<?php the_ID(); ?>">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
            <?php
    endwhile;
 ?>

EDIT 2018-10-31

I finally made it through native WP functions and a custom query. I also needed the pagination functionality so I built it this way.

    $termArray = [];
    $theTerms = get_terms('producers');
    foreach ($theTerms as $singleTerm) {
        $theSlug = $singleTerm->slug;
        array_push($termArray,$theSlug);
    }
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $loop = new WP_Query( array( 'post_type' => 'members', 'orderby' => 'rand', 'posts_per_page' => 5, 'paged' => $paged, 'tax_query' => array(
        array(
            'taxonomy' => 'producers',
            'terms'    => $termArray,
        )
    )));
    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post();
        $terms = get_the_terms( $post->ID, 'producers');
        if ($terms) {
            /// Here is the code for posts display
        }
    endwhile;
    endif;
    wp_reset_postdata();


// Pagination
    $big = 99999;
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $loop->max_num_pages
    ));
Share Improve this question edited Oct 31, 2018 at 16:27 Pierre asked Oct 29, 2018 at 20:57 PierrePierre 253 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The correct way to use the tax_query parameter is to have an array containing an array, like this:

'tax_query' => array(
    array (
        'taxonomy' => 'producers'
    )
),

See the Codex entry for more information.

Try this

<?php
$custom_args = array('post_type' => 'members',
                     'posts_per_page' => -1,
                     'orderby' => 'id',
                     'order' => 'ASC',
                     'tax_query' => array(array('taxonomy' => 'producers',
                                                'field' => 'slug',
                                                'terms' => 'your term name')));
$custom_query = get_posts($custom_args);
?>
<?php
    foreach ($custom_query as $value) 
     { ?>
       <li class="producers" id="post-<?php $value->ID ; ?>">
           <a href="<?php $value->guid ; ?>"><?php $value->post_title; ?></a>
       </li>
        <?php
     } ?>
Post a comment

comment list (0)

  1. No comments so far