one post per term taxonomx - wordpress

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.

Related

How to query Standard and Video Post Format in WordPress?

I am trying to query posts according to their post format. Currently, I have the video, gallery, and standard post formats on my site. It's quite easy to query these post formats individually. However, I have a combination of tax queries of 10 posts that are standard and video post format, my query fails.
Here's the code I have right now:
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'relation' => 'OR',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
I think, you should put the 'relation' => 'OR', into the 'tax_query'-Array.
You are adding 'relation' => 'OR' in the wrong place. it should be inside the tax_query array. check the below code.
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
USEFUL LINKS
taxonomy-parameters

How do I query posts that don't have both of two tags?

Say I have two tags, red and small. I want to get all posts that are not red and small. I want to include posts that are either red or small, but they can't be both.
The tag__not_in parameter won't work, because it'll exclude posts that are just red or just small.
I also tried the tax query below but then realized it's the same as the above.
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
'operator' => 'NOT IN',
),
)
I feel like there's got to be a way to do this and I'm just missing something simple. That and I'm getting confused by the logic inversions. Any ideas?
You can try again
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
),
)
);
$query_red_small = new WP_Query( $args );
$post_ids = wp_list_pluck( $query_red_small->posts, 'ID' );
$args = array(
'post_type' => 'post',
'post__not_in' => $post_ids
);
$query = new WP_Query( $args );
?>

Filter on Category and Postformat

I have just started in Wordpress Template development, I am getting used to it.
However I have been looking to find the solution here but unable to find the same question.
I would like to filter on Category and Post-Format, i have the following code the Filter on the Post Format works however not on the Category.
$videos = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-video'
),
'category_name=instructie-videos',
'posts_per_page=6',
'operator' => 'IN'
)
)
) );
$videos = new WP_Query( array(
'post_type' => 'your-post-type',
'posts_per_page' => 6,
'category_name' => 'instructie-videos',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' ),
'operator' => 'IN'
)
)
) );
maybe it's syntax mistake near category_name. Try above code
you should try as below:
$myposts = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-aside',
'post-format-audio',
'post-format-chat',
'post-format-gallery',
'post-format-image',
'post-format-link',
'post-format-quote',
'post-format-status',
'post-format-video'
),
'operator' => 'NOT IN'
)
)
) );

Dispaly only terms that has posts in Wordpress

I have a Wordpress post type that works with two taxonomies.
I want to display a list of all terms from taxonomy "locationcat", that uses the term "lunch" from the other taxonomy "menucat".
Sadly, I have being able to print all terms from both toxonomies..
Here is my code:
$terms = get_terms( array(
'hide_empty' => true,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'locationcat',
'hide_empty' => true,
),
array(
'taxonomy' => 'menucat',
'field' => 'slug',
'terms' => 'lunch',
'hide_empty' => true,
),
),
) );
foreach ($terms as $sc) {
$link = $sc->slug;
echo '<li>'.$sc->name.'</li>';
}
Your post is a bit confusing as you are asking "Dispaly only terms that has posts in Wordpress". I assume you meant "Display posts with terms from 2 taxonomies."
If this is correct then here is your solution.
$args = array(
'post_type' => 'your_post_type',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'tax_1'
'field' => 'slug',
'terms' => 'term-slug1'
),
array(
'taxonomy' => 'tax_2',
'field' => 'slug',
'terms' => 'term-slug1'
)
)
);
$posts = get_posts( $args );
And then proceed with your loop.
If I misunderstood your question then please try rephrasing it.

Woocommerce products query - order_by

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.

Resources