Skip to content
Advertisement

Recent posts being queried to Custom taxonomy post

I was trying to show posts from custom taxonomy to the index page but when I query the results posts that were recently added and has no relation to the taxonomy are also found.

$args = array(
             'post_type'         => 'post',
             'post_status'       => 'publish',
             'posts_per_page'    =>  4,
             'tax_query'         =>  array(
                                       'taxonomy' => 'city',
                                       'field' => 'id',
                                       'terms__in' => 227,
                                                )
);
$arr_posts = new WP_Query($args);

I also had the option to display recent posts on so I wonder if that contributes to the problem.

If so I was wondering the correct way to display recent post from custom taxonomy to the front page.

Advertisement

Answer

Your tax_query is incorrect. Please check with the below query.

$args = array (
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' =>  4,
    'tax_query' => array(
        array(
            'taxonomy'  => 'city',
            'field'     => 'term_id',
            'terms'     => array( 227 ),
        )
    ),
);
$arr_posts = new WP_Query($args);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement