Home › Forums › Support › exclude sibling terms from Next Previous Posts and related post query loop
- This topic has 23 replies, 3 voices, and was last updated 1 year ago by David.
-
AuthorPosts
-
March 30, 2023 at 7:06 pm #2589749TomLead DeveloperLead Developer
Hi there,
2. We use the following core functions to get the next and previous posts:
https://developer.wordpress.org/reference/functions/get_next_post/
https://developer.wordpress.org/reference/functions/get_previous_post/It looks like the same “In same term” option is what you’re after. That should limit posts to the current category. Is it not working?
3. How are you currently querying the posts here? In a Query Loop block? If so, what parameters are you using to query them?
March 31, 2023 at 5:57 am #2590520Brightest Media Sp. z o.o.Hi,
You can check my settings as I have sent the login details.
In general, it doesn’t work correctly because posts for the whole parent category and not for the sub category are showing. I would like the proposed posts to show only for the sub category.
Example:
https://swojewdomu.pl/ile-wazy-blacha-trapezowa/The parent category is “Budowa” and the sub category is “Dach”. In this case, the posts in the right sidebar should only display for the “Roof” category, and they display for the entire parent category, i.e. posts from the “Okna”, “Elewacja”, etc. category appear.
It cannot be that all posts appear there. I want only the posts for the subcategory in this case “Dach” to display.A screenshot to illustrate this – https://prnt.sc/O0S6xruVQDEH
Right sidebar settings – https://prnt.sc/CBF9XU5Q35uFThis is the situation with the navigation at the very bottom of the post. Articles link between the whole parent category, i.e. “Budowa”, and I would like the linking to be only between articles from the subcategory “Dach”.
A screenshot to illustrate this – https://prnt.sc/_z-M8gFlHjFl
Please check my settings that I applied to this website. I only used GeneratePress and GenerateBlocks here to create these elements.
If from the settings we can’t configure this properly then maybe you are able to give me such a filter in PHP that will adjust this filtering accordingly for these elements I described?
April 1, 2023 at 5:10 am #2591875DavidStaffCustomer SupportIt will need a PHP snippet to filter the query args of the loop block.
Try this:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { $class = 'cat-related'; $taxonomy = 'category'; // apply filter if loop class has matching $class if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], $class ) !== false ) { $terms = get_the_terms( get_the_ID(), $taxonomy ); $related_terms = array(); foreach ( $terms as $term ) { if ( $term->parent == 0 ) { // This is a parent term, exclude it continue; } else { // This is a child term, add it to the related terms array $related_terms[] = $term->term_id; } } // If there are no child terms, add all the terms to the related terms array if ( empty( $related_terms ) ) { $related_terms = wp_list_pluck( $terms, 'term_id' ); } // merge meta_key my-custom-field into query return array_merge( $query_args, array( 'tax_query' => array( array( 'taxonomy' => $taxonomy, 'field' => 'ID', 'terms' => $related_terms ) ) ) ); } return $query_args; }, 10, 2 );
NOTES:
Select the Grid block in the Query Loop and in Advanced > Additional CSS Class(es) add:
cat-related
You can change that class in the code above if you want on this line:$class = 'cat-related';
In the Query Loop Paramters you will need to remove the Taxonomy > Category -> Current Terms parameter as the filter above should replace that.
The logic of the code is this:
1. Get all terms of the current Post.
2. Loop through them and get any Terms that are NOT a parent and create our$related_terms
array.
3. If the$related_terms
are empty then use all$terms
4. Filter the tax query to show only posts whose tax matches$related_terms
IDs.It should work….
April 1, 2023 at 11:16 am #2592272Brightest Media Sp. z o.o.Hi David,
Thank you very much for the instructions it works very well on the right sidebar.
Then we are left with the last element of Post Navigation – https://prnt.sc/_z-M8gFlHjFl
Unfortunately, I was not able to apply your instructions to this element because the Post Navigation section does not have a Query Loop block. Do you perhaps have any idea how we can apply the same filter to these elements in Post Navigation?
April 3, 2023 at 2:52 am #2594271DavidStaffCustomer SupportJust to keep you updated, we’re looking at whether this is at all possible without rewriting the actual function.
The only alternative is to NOT select the Parent Category for the post….We’ll update asap.
April 4, 2023 at 2:29 am #2595949Brightest Media Sp. z o.o.In that case, I am waiting to hear if we can apply an additional filter to make it work as I described.
April 4, 2023 at 4:12 pm #2597135DavidStaffCustomer SupportOk, so after a lot of research and hair pulling the best I can offer is this function.
What it does is gets all the category terms of the current post.
Splits terms into (1) parent terms and (2) child terms
Loops through all Parent terms and gets ALL of its (3) child terms.
Create an array of (4) sibling terms ( which is 3 exclude 2 ).
If all that exists it passes those terms to theget_{adjacent}_post_excluded_terms
So in theory it should never show posts from sibling terms.
function exclude_sibling_terms() { // Get all category terms of the current post $categories = get_the_category(); // Create arrays to hold parent and child terms $parent_terms = array(); $child_terms = array(); // Loop through categories and separate into parent and child terms if ($categories) { foreach ($categories as $category) { if ($category->parent == 0) { $parent_terms[] = $category->term_id; } else { $child_terms[] = $category->term_id; } } } // Loop through parent terms and get their child terms $sibling_terms = array(); if ($parent_terms) { foreach ($parent_terms as $parent_term) { $children = get_term_children($parent_term, 'category'); if ($children) { foreach ($children as $child) { $sibling_terms[] = $child; } } } } // Remove any duplicate child terms from sibling terms array if ($child_terms && $sibling_terms) { $sibling_terms = array_diff($sibling_terms, $child_terms); } // Check if there are any sibling terms if (!empty($sibling_terms)) { return $sibling_terms; } // Return nothing if there are no sibling terms return; } // Exclude terms for the next post add_filter( 'get_next_post_excluded_terms', 'exclude_sibling_terms', 10, 1 ); // Exclude terms for the previous post add_filter( 'get_previous_post_excluded_terms', 'exclude_sibling_terms', 10, 1 );
April 6, 2023 at 5:36 am #2599377Brightest Media Sp. z o.o.Hi David,
It works. I’m very happy to see that you were able to solve the problems that appeared on my websites.
Thank you very much for your help and taking the time for my issue.April 7, 2023 at 3:22 am #2600688DavidStaffCustomer SupportI almost gave up lol, WP didn’t make that easy 🙂
Glad to be of help
-
AuthorPosts
- You must be logged in to reply to this topic.