0

I have a custom post type called anime and a taxonomy called series. If I view any post and that post belongs term Banana it should list all the posts in term Banana in a dropdown list. Here's the code so far:

<?php 
    $category = get_terms( 'series' );
    $catID = $category[0]->term_id;
    $args = array(
        'numberposts' => 5, 
        'category' => $catID,
        'post_type' => 'anime'          
    );
    $catPosts = get_posts( $args );

    echo '<form method="POST">';
    echo '<select name="goToPost" onchange="document.location=this.value">';
    echo '<option value="">'.__('Relevent Posts', 'your_text_domain').'</option>';  
    foreach( $catPosts as $singlePost ) {
    echo '<option value="'.get_bloginfo('url').'/index.php?p='.$singlePost->ID.'">'.$singlePost->post_title.'</option>';
    };
    echo '</select>';
    echo '</form>';
?> 

But it's not working right. it's basically listing all posts in the taxonomy and not within the term

1

You should be using a tax_query for this operation. The category parameters won't work with custom taxonomies

Instead of

'category' => $catID,

use

'tax_query' => array(
        array(
            'taxonomy'          => 'series',
            'field'             => 'term_id',
            'terms'             => $catID,
            'include_children'  => false
        ),
    ),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.