How to show all custom post from custom taxonomy? - wordpress

I have a custom post called item and custom taxonomy under this custom post called item_category
Now, I want to get all posts based on this custom taxonomy.
To do that, I have this URL of custom taxonomy:
http://localhost/mysite/item_category/event/
Here, item_cateogry is the custom taxonomy and event is the term of this taxonomy.
So, my code is below but not showing anything.
$queried_object = get_queried_object();
$current_category_name = $queried_object->slug; // will print the `event`
$items = new WP_Query(array(
'post_type' => 'item',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $current_category_name,
'field' => 'slug',
),
),
));

I have found the issues:
Here is the right code:
$items = new WP_Query(array(
'post_type' => 'item',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'item_category',
'field' => 'slug',
'terms' => $current_category_name
),
)
));

Related

How to exclude entries with custom taxonomy from the main query

I need to exclude custom post type entries with a specific custom taxonomy from the main query. I have reviewed other similar entries on SE but can't make it work.
This is what I have:
$args = array(
'post_type' => 'pojazdy', //my cpt name
'tax_query' => array(
array(
'taxonomy' => 'oferta_aktywna', //my custom taxonomy name
'field' => 'slug',
'terms' => 'sprzedane', //specific custom category
'operator' => 'NOT IN',
),
)
);
$query = new WP_Query( $args );

how to call optiontree taxonomy select

I ask for help to solve my problem, I use the option-tree framework for displaying custom posts from specific categories, I have this code:
<?php
global $product;
$category_product1 = ot_get_option( 'homepage-slider-cat1' );
$args = array(
'post_type' => 'product',
'numberposts' => 18,
'post_status' => 'publish',
'category' => $category_product1,
'include_children' => true
);
?>
with this post code specific category does not appear.
Please help me
Have you tried the Tax_query as a query parameter? This way you can query on taxonomies.
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Hide custom taxonomy posts from Recent posts

I have some custom taxonomy in my WordPress site, but I want to hide a custom taxonomy post from my Recent posts. I have this code:
$c1 = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'quality',
'field' => 'id',
'terms' => 123,
))
);
$c2 = array(
'post_status' => 'publish',
'post__not_in' => $c1,
'posts_per_page' => 10,
);
$recent = new WP_Query($c2);
while ($recent->have_posts()) : $recent->the_post();
But unfortunately it doesn't work. How should I do this?

Query meta data in taxonomony using plugin Advanced custom fields

I want to run a query in wordpress to find all local services posts in a taxonomy called local area by the meta data. I have created a custom field using the advanced custom field plugin with the field name as area_code. I also have a drop down select box with service types.
<?php
$args = array(
'post_type' => 'local_service',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'or',
array(
'taxonomy' => 'service_type',
'field' => 'name',
'terms' => array( $catagory ),
),
array(
'taxonomy' => 'local_area',
'meta_key' => 'area_code',
'meta_value' => $ons_code,
'meta_compare' => '=',
),
),
);
$loop = new WP_Query( $args );
?>

Wordpress query not filtering by taxonomy when specifying a slug

I have a custom post type called news. Inside that I have two custom taxonomies called press-releases and media-coverage. When I query for multiple posts with this query:
$args = array(
'post_type' => 'news',
'news_type' => 'press-releases',
'posts_per_page' => 15,
'paged' => $json_api->query->page,
);
$query = new WP_Query($args);
It only gets news posts categorized as press-releases like I want. But if I specify a slug that exists in media-coverage it returns that post even though I have the press-releases taxonomy specified. Example:
$args = array(
'name' => $slug,
'post_type' => 'news',
'news_type' => 'press-releases',
'posts_per_page' => 1,
);
You may try this
$args = array(
'post_type' => 'news',
'name' => '$slug,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_type',
'field' => 'slug',
'terms' => 'press-releases'
)
)
);

Resources