custom query for taxonomy - php

I am using this query to show certain posts.
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
APP_TAX_STORE => $term->slug,
),
) );
Now instead of showing all posts from APP_TAX_STORE => $term->slug I would like to exclude them. I tried all solutions found in this form, but nothing worked. Any Ideas?

You can use tax_query.
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => APP_TAX_STORE,
'field' => 'slug',
'terms' => $term->slug,
'operator' => 'NOT IN',
),
),
) );

Related

Woocommerce, get products by category with wp query

I tried many methods, but I can't show products with category name or id
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'cat' => 40,
'posts_per_page' => '12',
);
$loop = new WP_Query( $args );
Try this example,
So given a category with ID 26, the following code would return it's products (WooCommerce 3+):
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products = new WP_Query($args);
var_dump($products);
Try tax_query
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array('40'),
'operator' => 'IN',
)
)
);
$loop = new WP_Query( $args );
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Try this,
<?php
$args = array( 'post_type' => 'product', 'post_status' => 'publish','category' => 34, 'ignore_sticky_posts' => 1, 'posts_per_page' => '12',);
$products = get_posts( $args );
?>
Hope this will helps you. for more info.
I suggest used >> wc_get_product_terms( $vars->ID, 'pa_brand_category' )
please check this screenshot >> http://prntscr.com/hjawu5

woocommerce product attribute, how to filter between two values

Lets say i have a list off houses and they have a attribute called "size" now I want to get all houses between size 200 and 300.
I have tried
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'pa_size',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
);
Then I tried with tax_query but I couldn't find a way to get a term between two values.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => $sizevalue
)
);
);
Can't understand if this should not be possible but I think the value has to be a string therefor it cant be between.
for now im sorting them in my foreach loop when im displaying them but then my pagination is not working.
My conclusion was that you cant do this with woocommerce product attributes because they a text based, så I made some Advancec custom fields and used them insted like this
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND,
array(
'key' => 'myCutsomField',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'myCutsomField2',
'value' => array($valueMin, $valueMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$products = new WP_Query( $args );

Woocommerce: Create short-code with parameters

I am trying to create a short-code to get products from specific category in woocommerce. I am using 'tax_query' to target specific category and 'shortcode_atts' to get parameter from shortcode itself. The code is as follow:
function quick_launch_products($atts) {
extract(shortcode_atts(array(
'product_category_ID' => '',
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_category_ID,
'operator' => 'IN'
)
)
);
echo $product_category_ID;
$products = null;
$products = new WP_Query($args);
}
add_shortcode("quick_launch_product_slider", "quick_launch_products");
The shortcode:
[quick_launch_product_slider product_category_ID="383"]
The return value is blank. I saw a lot of demo codes and followed exactly as they were, but its not working at all.
What am i missing here?
Thanks in advance.

WP query if in category OR has taxonomy

I need to figure out how to query posts by the following conditions:
if they are in the category (by ID) or if they have a taxonomy term. So that on one archive page, I have all posts with a category ID of 12, and all posts that have the taxonomy term 'archive-trading-tools' I have set up custom taxonomy and that part is working. I am stuck at:
<?php
$query_args = array(
'post_type' => array('post', 'webinar'),
'posts_per_page' => 10,
'paged' => $paged,
'page' => $paged,
'cat' => 12,
//'relation' => 'OR'
);
$query_args['tax_query'] = array (
'taxonomy' => 'archive-categories',
'term' => 'archive-trading-tools'
);
$the_query = new WP_Query( $query_args );
?>
In WordPress a category is a built-in taxonomy. Then in theory, the following should work.
$args = array(
'post_type' => array('post', 'webinar'),
'posts_per_page' => 10,
'paged' => $paged,
'page' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'archive-categories',
'field' => 'slug'
'terms' => 'archive-trading-tools',
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 12
)
)
);
$query = new WP_Query( $args );
To expand on #depiction's answer, you the 'terms' in the tax_query should be "term" instead of "terms". No 's at the end.
$args = array(
'post_type' => array('post', 'webinar'),
'posts_per_page' => 10,
'paged' => $paged,
'page' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'archive-categories',
'field' => 'slug'
'terms' => 'archive-trading-tools',
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 12
)
)
);
$query = new WP_Query( $args );

WordPress Loop Not Respecting Post Status

I have a series of loops spitting out 'event' custom post types that I want to only pull posts with a post_status of 'publish'. I've added the post_status of 'publish' in my wp_query array but it doesn't seem to work. I still have scheduled posts showing up.
<?php
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'meta_key' => '_expiration_date',
'orderby' => 'meta_value',
'order' => 'ASC',
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
$wp_query4 = new WP_Query($args_hotel_feature);
if($wp_query4->have_posts()) :
while($wp_query4->have_posts()) :
$wp_query4->the_post();
?>
Anyone else experienced this issue?
Use meta_query
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
)
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
Turns out the query was fine, however, the post expiration plug-in "Posts Expiration Date" breaks post_staus. Use "Post Expirator" instead.

Categories