最新消息: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)

custom taxonomy - Related posts by taxonomies does not work , what could be the reason?

matteradmin7PV0评论

everybody. Related posts by taxonomies does not work , what could be the reason?

$tax_IDs = array();
foreach ($tags as $tag) {
    $tax_IDs[] = $tag->ID;
}

$args = array(
  'post_type' => 'films',
  'post__not_in' => array($post->ID),
  'showposts'=> 3,
  'tax_query' => array(
        array(
            'taxonomy' => 'actors',
            'field' => 'id',
            'terms' => $tax_IDs
        )
    )
);
 $my_query = new wp_query($args);
 if( $my_query->have_posts() ) {

 echo '<div class="related-slider">';
        while ($my_query->have_posts()) {
            $my_query->the_post();
        ?>
            <div><?php the_title(); ?></div>       
        <?php
        }
        echo '</div>';
    }
 wp_reset_query();

everybody. Related posts by taxonomies does not work , what could be the reason?

$tax_IDs = array();
foreach ($tags as $tag) {
    $tax_IDs[] = $tag->ID;
}

$args = array(
  'post_type' => 'films',
  'post__not_in' => array($post->ID),
  'showposts'=> 3,
  'tax_query' => array(
        array(
            'taxonomy' => 'actors',
            'field' => 'id',
            'terms' => $tax_IDs
        )
    )
);
 $my_query = new wp_query($args);
 if( $my_query->have_posts() ) {

 echo '<div class="related-slider">';
        while ($my_query->have_posts()) {
            $my_query->the_post();
        ?>
            <div><?php the_title(); ?></div>       
        <?php
        }
        echo '</div>';
    }
 wp_reset_query();
Share Improve this question edited Mar 23, 2019 at 0:45 starspro asked Mar 23, 2019 at 0:13 starsprostarspro 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Your tax_query is using an invalid field type. Try:

$args = array(
  'post_type' => 'films',
  'post__not_in' => array($post->ID),
  'showposts'=> 3,
  'tax_query' => array(
        array(
            'taxonomy' => 'actors',
            'field' => 'term_id',
            'terms' => $tax_IDs
        )
    )
);

See the Taxonomy Parameters of WP_Query for more options if that doesn't wok.

Post a comment

comment list (0)

  1. No comments so far