Display recent posts from a custom taxonomy in wordpress

Vido900

Junior Member
Joined
Mar 2, 2021
Messages
162
Reaction score
54
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.
 
Try this:

Code:
$the_query = new WP_Query( array(
    'post_type' => 'Adverts',
    'tax_query' => array(
        array (
            'taxonomy' => 'advert_tag',
            'field' => 'slug',
            'terms' => 'politics',
        )
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    // Show Posts ...
endwhile;

/* Restore original Post Data
 * NB: Because we are using new WP_Query we aren't stomping on the
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();

Source: https://wordpress.stackexchange.com/a/84923
 
Back
Top