Skip Navigation

[Resolved] View with Taxonomy Filter “Set by current archive page” not working

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 1 voice.

Last updated by Robert 1 day, 12 hours ago.

Assisted by: Minesh.

Author
Posts
#2793860

I am trying to display related posts of the same taxonomy on the taxonomy archive page. I have created a custom Taxonomy called "Locations" that is hierarchical. For example:

Washington
-- King County
-- Seattle

For the posts I created, I have checked ONLY the Seattle term for locations on several posts, not the King County and Washington parent terms.

I have also created a view to display posts with a Taxonomy filter set to "Set by the current archive page". So my expectation is that when I load archive pages, I will either see Seattle posts or not. For example:

Expectation:
Washington Archive Page - Should not see Seattle posts
-- King County Archive Page - Should not see Seattle posts
-- Seattle - Should See Seattle posts

However, I see the Seattle posts on all 3 levels of the Archive pages.

Furthermore, I would expect that if my post has both Seattle and King County selected, then the corresponding archive pages for Seattle and King County would also show those posts while the Washington archive page would not (since I didn't select Washington)

How do I show posts that only have on the archive pages where I have only selected the same taxonomy (e.g. Seattle selected on the Seattle Archive page and not the parent pages)?

#2793862

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To filter the archive you will require to use the WordPress standard hook pre_get_posts.

Please check the following related links that might help you:
- https://wordpress.stackexchange.com/questions/55257/exclude-child-term-posts-from-parent-term-archive
- https://wordpress.stackexchange.com/a/140952

#2793866

Thanks Minesh. Most of those didn't work and had syntax errors that broke the site. This one from your second link sort of works:
https://wordpress.stackexchange.com/a/140952

function kia_no_child_taxonomies( $query ) {

if( is_tax() ):

$tax_obj = $query->get_queried_object();

$tax_query = array(
'taxonomy' => $tax_obj->taxonomy,
'field' => 'slug',
'terms' => $tax_obj->slug,
'include_children' => FALSE
);
$query->tax_query->queries[] = $tax_query;
$query->query_vars['tax_query'] = $query->tax_query->queries;

endif;

}
add_action( 'pre_get_posts', 'kia_no_child_taxonomies' );

However, now it only shows on the child page. If I have a post that has Thurston County AND Seattle selected, it will only show on the Seattle archive page, not the Thurston County page.

Thoughts?

#2793867

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can I've example URLs on what URL you want to include the child term post or on what URLs it should not display what posts and what not and share admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2793870

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please clarify what posts should be displayed on all those three links you shared:
- hidden link
- hidden link
- hidden link

#2793872

- hidden link - Should display "Mortgage Calculator" only
- hidden link Should display "Mortgage Calculator" only
- hidden link Should display "Mortgage Calculator" AND "About Yelm"

#2793874

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Do you mean that when displaying any of the child term archive page - you want to dispaly parent term posts as well on child term archive page.

Lets consider the following example:
hidden link
- only posts belongs to term "washington"

- hidden link
On above child term archive page you want to display posts belongs to term:
thurston-county + ( washington) (if any post attached to parent term)

- hidden link
On above child term archive page you want to display posts belongs to term:
yelm + ( thurston-county + washington) (if any post attached to these parent terms)

#2793875

No, just the term that is selected:

hidden link
- only posts belongs to term "washington"

hidden link
- only posts belongs to term "thurston-county"

hidden link
- only posts belongs to term "yelm"

#2793876

But just for this particular view. For other views I am displaying children taxonomies. I have a view called "Child Taxonomy Links" that works fine as is.

#2793878

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

On this page:
- hidden link

It does display the "Mortgage Calculator" AND "About Yelm".

#2793884

yes

#2793887

But its displaying them on the other pages when it shouldnt

#2793897

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As you are using the following view to filter the post based on the current post archive term:
=> hidden link

You should remove any other code you added as I've added the following view's filter code to exclude the child to "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query', 'func_exclude_children_post__view_taxonomy', 99, 3 );
function func_exclude_children_post__view_taxonomy( $query_args, $view_settings, $view_id ) {
  
   $target_view_ids = array(617);
    if (in_array($view_id,$target_view_ids) and !empty($query_args['tax_query']) ){
        $query_args['tax_query'][0]['include_children'] = 0;  
    }
    return $query_args;
}

Can you please confirm it's working as expected.

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2793910

I solved this but you don't want to know how I did this.