Hello everyone, you need to make a selection of posts custom taxonomy with a specific id. (Taxonomy ID) Following the documentation at https://wp-kama.ru/function/wp_query#parametry-taksonomij his example:
$query = new WP_Query( array( 'tax_query' => array( array( 'taxonomy' => 'people', 'field' => 'slug', 'terms' => 'bob' ) ) ) ); I'm trying to repeat, in my case we have taxonomy - job_listing_category post_type - job_listing and id of a specific category for selecting posts for example 1743 so the code
$args = [ 'post_type' => 'job_listing', [ 'tax_query ' => [ 'taxonomy' => 'job_listing_category', 'fields' => 'term_id', 'terms' => '1743' ] ] ]; $q = new WP_Query( $args ); debug($q->posts); And in this case, I get all the posts from the taxonomy of job_listing_category but the 'terms' => '1743' does not work. The question is why is this happening?
Taxonomy data taken from the address bar - screenshot
Did I choose the right data?
Then I was prompted to make another request to the class WP_Query as follows:
$args1['tax_query'][] = [ 'taxonomy' => 'job_listing_category', 'field' => 'term_id', 'terms' => 1743 ]; Thus, the necessary category is triggered and displayed, but this is contrary to the documentation. In confusion, I can help you understand why this happens :)
Well, everything that goes further after the WP_Query () request doesn’t interest us, so the code is essentially like this:
function inpoland_job_categories_cb(){ /** * Такой запрос не срабатывет */ $args1 = [ 'post_type' => 'post', 'tax_query' => [ 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => '4088', ], ]; /** * Запрос который мне подсказали */ $args2['tax_query'][] = [ 'taxonomy' => 'job_listing_category', 'field' => 'term_id', 'terms' => '1743', 'post_per_page' => 2 ]; /** * Правильной запрос */ $args3 = [ 'post_type' => 'job_listing', 'tax_query' => [ [ 'taxonomy' => 'job_listing_category', 'fields' => 'id', 'terms' => '1743', ] ], 'post_per_page' => '20' ]; $q = new WP_Query( $args3 ); debug($q->posts); //просто вывод на экран } add_shortcode('inpoland_job_categories', 'inpoland_job_categories_cb');