I need to exclude a category from showing up posts.
I registered the taxonomy: portfolio-category
and added a category: accessories (cat ID 19) under portfolio-category
How do I exclude posts from accessories category from showing up?
I tried: 'category' => -19, but it didn't work
here's my code:
<?php
$args=array(
'post_type' => 'items',
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3) ),
'caller_get_posts' => 1,
'category' => -19,
'paged' => $paged,
);
query_posts($args);
$end = array(3,6,9,12,15,18,21,24,27,30,33,36,39,42,45);
$i = 0;
while (have_posts()): the_post();
global $post;
$i++;
?>
MY CODE HERE, NO NEED TO SHOW SINCE IT'S VERY LONG
<?php endwhile; ?>
<?php wp_reset_query(); ?>
edit// I tried this code, but still it didn't work:
<?php
$args=array(
'post_type' => 'items',
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3) ),
'caller_get_posts' => 1,
'paged' => $paged,
'tax_query' => array(
'taxonomy' => 'portfolio-category',
'terms' => 'accessories',
'field' => 'slug',
'operator' => 'NOT IN')
);
query_posts($args);
$args = array(
'post_type'=>'items',
'order'=>'ASC',
'posts_per_page'=>3
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'id',
'terms' => 19,
'operator' => 'NOT IN',
),
)
));
query_posts($args);
items= custom post type
portfolio-category = my custom taxonomy
for multiple category exclude use 'terms' => array( '19,20' ),
The category argument is meant for the built in category taxonomy. Change your $args like so to reference your custom taxonomy:
$args=array(
'post_type' => 'items',
'portfolio-category' => 'accessories',
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3)),
'paged' => $paged
);
This assumes the following:
You've got a custom post type called items.
The portfolio-category taxonomy is registered to it.
accessories is added to the portfolio-category taxonomy.
Update: Whoops...getting late. To answer OP's actual question of how to exclude the accessories portfolio category (rather than include it as the above does), you can use the tax_query argument. Code would be as follows to exclude accessories:
$args=array(
'post_type' => 'items'
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3)),
'paged' => $paged,
'tax_query' => array(
'taxonomy' => 'portfolio-category',
'terms' => 19,
'field' => 'id',
'operator' => 'NOT IN'
)
);
The issue seems to be a layer of nesting. Try changing
$args=array(
'post_type' => 'items'
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3)),
'paged' => $paged,
'tax_query' => array(
'taxonomy' => 'portfolio-category',
'terms' => 19,
'field' => 'id',
'operator' => 'NOT IN'
)
);
To:
$args=array(
'post_type' => 'items'
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3)),
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'terms' => 19,
'field' => 'id',
'operator' => 'NOT IN'
)
)
);
This worked for me. :-/
Related
I'm trying to pull all products located within a certain category slug.
I've tried the following, however both return every product in the store.
$products = wc_get_products( array( 'category' => array( 'Sony' ) ));
and
$productlist = wp_query(array( 'post_type' => 'product', 'product_cat' => 'Sony'));
Any pointers would be much appreciated.
Try this code
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => 12,// category ID
),
),
);
Try This:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'Sony', 'orderby' => 'rand' );
$data = new WP_Query( $args );
print_r($data);
Try with this and replace ENTER_CATEGORY with slug name or use simple 'terms' => 'slug/categoryname'
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
'field' => 'slug',
'operator' => $atts['operator']
)
)
);
I need some help, again.
I use this query:
$tax = get_the_terms($id, 'coupon_category');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'coupon_category', // Taxonomy
'field' => 'slug',
'terms' => $tax[0]->slug, // Your category slug
),
array(
'taxonomy' => APP_TAX_STORE,
'field' => 'slug',
'terms' => $term->slug,
'operator' => 'NOT IN',
),
),
) );
I know need a solution to only show one post from each Taxonomy term APP_TAX_STORE. Tried some solutions from this userfull forum but non worked for me.
I'm hoping someone can help spot what the problem is. I have a custom taxonomy called "events_cat" I am trying to get all the post types "event" that are in the taxonomy term 11 to display, however the below code is pulling in events that are not in that taxonomy term and I can't see the mistake. Any ideas what might be causing the issue?:
<?php
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'events_cat',
'field' => 'term_id',
'terms' => array(11),
),
));
$upcomingEvents = new WP_Query($args); ?>
$custom_args=array(
'post_type' => "event",
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> -1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'id',
'terms' =>"11"
)
),
'orderby' => 'id',
'order' => 'ASC'
);
$custom_my_query = null;
$custom_my_query = new WP_Query($custom_args);
$custom_my_total_count = count($custom_my_query);
if( $custom_my_query->have_posts() )
{
while ($custom_my_query->have_posts()) : $custom_my_query->the_post();
?>
<?php echo get_the_title($post->ID);?>
<?php
endwhile;
}
wp_reset_query($custom_my_query); // Restore global post data stomped by the_post().
}
I am using a query in my template.php file to display wooCommerce products by category.
My query looks like this:
<?php
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 99,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'system_components',
'menu_order' => 'asc'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'add-ons',
'menu_order' => 'asc'
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
)
));
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) :
$loop->the_post(); ?>
.....
This works, and I can see the products I want and the one I don't want are not visible.
The only bit I don't understand is that 'menu_order' => 'ASC' doesn't seem to work.
I doesn't' matter what I type as menu order in the product settings, the order doesn't change.
What am I doing wrong here?
Thanks
Remove 'menu_order' => 'asc' part from tax_query and add to main, that should work:
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 99,
'orderby' => 'menu_order',
'order' => 'ACS',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'system_components'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'add-ons'
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
)
));
As you can see in the documentation, the tax_query hasn't parameter menu_order.
Please, Set page order in your product.
For this go to Admin panel. Click on Products menu from left menu, Then edit product and set page order.
I have a simple WP_query on a custom post and works fine:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => 'london'
);
but I want it to query two locations so used an array:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => array('london','paris')
);
but it only pulls in the last term. Is there another way that I should be doing this?
$args = array(
'post_type' => 'post-type',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => array('london','paris')
)
)
);
Try this