Dispaly only terms that has posts in Wordpress - 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.

Related

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

Can you use tax_query with multiple post types in wp_query?

Let's say I'm querying two post types, like so:
$args = array(
'post_type' => array('post', 'another_post_type'),
'tax_query' => array(
array(
'taxonomy' => 'custom-taxonomy',
'field' => 'slug',
'terms' => 'test-slug',
)
)
);
If I have custom-taxonomy linked to another_post_type, how would I run this query so that the tax_query only ran for the another_post_type posts? Basically I want the query to return all regular post, but only another_post_type posts with the category of test-slug.
Is this possible?
This seems to be working:
$args = array(
'post_type' => array('post', 'another_post_type'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'custom-taxonomy',
'field' => 'slug',
'terms' => 'test-slug'
),
array(
'taxonomy' => 'category', # default post category
'operator' => 'EXISTS'
)
)
);

one post per term taxonomx

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.

WP_Query Multiple Categories

I've My categories Structure like this :
Music
-Genre
-Pop
-Rock
-Role
-Vocalist
-Guitarist
So Now I want to filter posts in such a way that a post should belong to ( either Pop or Rock) and (Vocalist)
So an or between genres and an and with the role
So speaking in wordpress terms in need multiple category_in or category_and which has category_in
Tricky right?
Any solution?
Try this and see if it works.
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'vocalist' ),
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'pop' ),
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'rock' ),
),
),
),
);
$query = new WP_Query( $args );

Resources