how to call optiontree taxonomy select - wordpress

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

Related

WP_Query not working with tax_query and woocommerce product attribute

Im trying to make ajax filter by attributes for woocommerce.
The problem is query giving empty result, when I'm adding tax_query. This code goes in functions.php
$newquery = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => '22',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'pa_chrisi', // Product attribute taxonomy: always start with 'pa_'
'field' => 'term_id', // Can be 'term_id', 'slug' or 'name'
'terms' => 170,
), ),
) );
I've checked the database for pa_chrisi taxonomy.
I've tried:
to init woocommerce (not sure why) before my code.
to add 'suppress_filters' => true, and 'include_children' => false,
echo $newquery->request; gives me
SELECT SQL_CALC_FOUND_ROWS dmg0j_posts.ID FROM dmg0j_posts WHERE 1=1 AND ( 0 = 1 ) AND dmg0j_posts.post_type = 'product' AND ((dmg0j_posts.post_status = 'publish')) GROUP BY dmg0j_posts.ID ORDER BY dmg0j_posts.post_date DESC LIMIT 0, 22
If I remove tax_query part it works well. Im getting all products.
Please help me!
solution:
if you are using WP_Query with tax_query in functions.php put the code inside an action with low priority:
add_action( 'init', 'maratgenius', 999 );
function maratgenius()
{
$newquery = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => '-1',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'pa_chrisi', // Product attribute taxonomy: always start with 'pa_'
'field' => 'term_id', // Can be 'term_id', 'slug' or 'name'
'terms' => $_POST['get_what'],
), ),
) );
//other code
}
As alternative you can register WC taxonomies before query via:
\WC_Post_Types::register_taxonomies();

Wordpress: Multiple taxonomy terms in WP_Query

I am trying to get multiple taxonomy terms in WP_Query. Here is my code:
$category = get_field('portfolio_category'); //array of IDs like 14, 15, 16
<?php
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => array( implode(', ', $category ) ),
'operator' => 'AND'
)
),
));
?>
The problem is in the current code I can only query the very first term.
For example I can only get projects from cat ID=14.
What am I doing wrong here? How can I query posts from multiple terms?
Thank you.
Since $category is already an array... change your terms value to just $category. so your full query would be:
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => $category,
'operator' => 'AND'
)
),
));

How to show all custom post from custom taxonomy?

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
),
)
));

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?

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