admin管理员组

文章数量:1291044

I create a custom taxonomy called "site". I cannot figure out what code to put in single.PHP to show related posts ONLY from "site" custom taxonomy.

Please help.

I create a custom taxonomy called "site". I cannot figure out what code to put in single.PHP to show related posts ONLY from "site" custom taxonomy.

Please help.

Share Improve this question asked Jun 12, 2021 at 9:12 Alex897Alex897 1 1
  • Does this answer your question? Show related posts on single page by custom taxonomy on custom post – bosco Commented Jun 12, 2021 at 15:43
Add a comment  | 

1 Answer 1

Reset to default 0

What you need is to add a tax_query to your WP_Query arguments, where you check that your taxonomy exists on the posts you are querying.

Here is an example that fetches the 3 latest published posts that have the taxonomy site set.

$args      = array(
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'posts_per_page' => 3,
    'tax_query'      => array(
        array(
            'taxonomy' => 'site',
            'operator' => 'EXISTS',
        ),
    ),
);
$the_query = new WP_Query( $args );

Good reads

  • Documentation for WP_Meta_Query
  • Documentation for WP_Query

本文标签: Show recent posts from a custom taxonomy in wordpress