Tagged: Custom Post Type, exclude, loop, masonry
-
AuthorPosts
-
February 14, 2022 at 4:08 pm #1340523
Dear Support-Team,
I do have a question and kind of found some solutions, but not the final one yet. In a project I use the same tags across the website but in the masonries I only want to show some of the contents attached with the tags.
I found this code:
add_filter('avia_masonry_entries_query', 'avia_masonry_custom_query'); function avia_masonry_custom_query( $query ) { $exclude_cat = array('category__not_in' => 'XXX'); $query = array_merge((array)$exclude_cat, (array)$query); return $query; }
Works like charm. If a have a post with a “hidden” category and a certain tag attached I don’t see the posts in the masonry.
Now it is getting complicated: I do have a Custom Post Type with enabled tags. I don’t want to show posts in the CPT with attached tags in the masonries. Therefore I found this code:
function avia_masonry_custom_query( $query ) { $query['tax_query'][] = array( 'taxonomy' => 'YYYYYYYYY', 'field' => 'id', 'terms' => ZZZ, 'operator' => 'NOT IN' ); return $query; } add_filter('avia_masonry_entries_query', 'avia_masonry_custom_query');
Kind of works but the new query is not rendered in the correct way. The posts get excluded but the masonry is not looking correct.
How can I combine those to arrays into one filter like this:
In Masonry sorted by tag: Show all entries with tags
but don’t show entries with tags but within category “XXX”
and don’t show entries with tags but in CPT “YYYYYYYYY”Any advice?
All the best, Daniel
February 14, 2022 at 11:50 pm #1340605Hey spooniverse,
Thank you for the inquiry.
You can combine multiple taxonomy queries (tax_query) using the key “relation” with the value AND or OR. Please check the code examples in the following documentation.
// https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
Best regards,
IsmaelFebruary 15, 2022 at 9:40 am #1340655Hi Ismael,
that is correct but I want to mix (standard) categories and taxonomy entries. How can I do that? Something like that is not working:
function avia_masonry_custom_query( $query ) { $args = array( 'meta_query' => array( 'relation' => 'OR', array( 'post_type' => 'kanal', 'taxonomy' => 'kanal_zuordnung', 'field' => 'id', 'terms' => array('30', '31'), 'operator' => 'NOT IN' ), array( 'post_type' => 'post', 'category__not_in' => '1', ), ), ); $query = array_merge((array)$args, (array)$query); return $query; } add_filter('avia_masonry_entries_query', 'avia_masonry_custom_query');
In the end I could even exclude the whole Custom Post Type, that would be okay. So if I could exclude a certain (standard) category and a certain CPT everything should be fine. Do you have a code snippet for me?
Best regards,
DanielFebruary 16, 2022 at 6:20 am #1340835Hi,
Thank you for the update.
You should use the tax_query parameter, not the meta_query and please note that the taxonomy queries doesn’t accept post_type as a parameter. This is an example from the documentation.
$args = array( 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'movie_genre', 'field' => 'slug', 'terms' => array( 'action', 'comedy' ), ), array( 'taxonomy' => 'actor', 'field' => 'term_id', 'terms' => array( 103, 115, 206 ), 'operator' => 'NOT IN', ), ), );
Best regards,
IsmaelFebruary 19, 2022 at 10:34 pm #1341436Hey Ismael,
now I have written to filters:
function avia_masonry_custom_cat_query( $query ) { $exclude_cat = array('category__not_in' => array( 1 )); $query = array_merge((array)$exclude_cat, (array)$query); return $query; } add_filter('avia_masonry_entries_query', 'avia_masonry_custom_cat_query'); function avia_masonry_custom_tax_query( $query ) { $query['tax_query'][] = array( 'taxonomy' => 'kanal_zuordnung', 'field' => 'id', 'terms' => array('30', '31'), 'operator' => 'NOT IN' ); return $query; } add_filter('avia_masonry_entries_query', 'avia_masonry_custom_tax_query');
The Category and Taxonomy Terms are now excluded. Works fine. Two questions:
• Can I somehow exclude the complete Custom Post Type or do I have to define the Terms that should be excluded from masonries (like in the code above)?
• Can I combine the two filters into one?Best regards,
DanielFebruary 21, 2022 at 5:14 am #1341572Hi,
Can I somehow exclude the complete Custom Post Type
You can unset the custom post type in the query. By default, all post types are included in the query and we use the tax_query to control which posts to actually retrieve.
Example:
function avia_masonry_custom_tax_query($query) { // this unsets the post type with the slug "name_of_post_type" unset($query["post_type"]["name_of_post_type"]); $query['tax_query'][] = array( 'taxonomy' => 'kanal_zuordnung', 'field' => 'id', 'terms' => array('30', '31'), 'operator' => 'NOT IN', ); return $query; } add_filter('avia_masonry_entries_query', 'avia_masonry_custom_tax_query');
Make sure to adjust the post type name in the filter.
Best regards,
IsmaelFebruary 21, 2022 at 12:31 pm #1341616Nice, Ismael, thank you! Now I have:
function avia_masonry_custom_query( $query ) { unset($query['post_type']['slug']); // Exludes Custom Post Type by slug $exclude_cat = array('category__not_in' => array( 1 )); // Excludes category by ID $query = array_merge((array)$exclude_cat, (array)$query); return $query; } add_filter('avia_masonry_entries_query', 'avia_masonry_custom_query');
That works as expected. You may close this thread as resolved.
- This reply was modified 2 years, 9 months ago by spooniverse.
-
AuthorPosts
- The topic ‘Exclude Custom Post Type from Masonry Grid’ is closed to new replies.