Skip Navigation

[Resolved] Filter CPT by grandparent CPT

This thread is resolved. Here is a description of the problem and solution.

Problem: I created a View of results filtered by post relationship. When this View is shown on the site, all results are shown until someone selects a filter. I would like to only show results that belong to a specific grandparent.

Solution: You can use a URL parameter to predefine a grandparent post ID, or you can add some custom code that hides all results until at least one filter is selected. Here is that custom code:

add_filter( 'wpv_filter_query_post_process', 'drop_empty_search_query_post_process', 10, 3 );
function drop_empty_search_query_post_process( $query, $view_settings, $view_id ) {
    $ids = array(1234,5678);
    if (in_array($view_id, $ids)){
      if (
        // taxonomy filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-relationship-filter-fr-region']) && $_GET['wpv-relationship-filter-fr-region'] != '0' )
          ||
        // taxonomy filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-relationship-filter-int-country']) && $_GET['wpv-relationship-filter-int-country'] != '0' )
      ) {
      } else {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Replace 1234,5678 with a comma-separated list of Views where you want to apply this custom filter.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Sebastien 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#613866
180207-toolset-1.jpg
180207-toolset-3.jpg
180207-toolset-2.jpg

Hello,

I'm building a professional directory website. In fact there will be 2 subdirectories, one for France and another for expatriate (international).
Before setting up the production site, I created a test site with dummy content to be sure I'm building things the right way.

I started with creating the following CPT
Child post type : Professional
Parent Post types : FR Cities and INT Cities
Grandparent Post types : FR Region (parent of FR Cities) and INT Countries (parent of INT Cities).

A professional can be listed in both directories, in other words a Professional post can have one or two parents (FR City post and/or INT City post)
In the end, I'd like to allow professionals to be listed in more than one city of each (FR and INT), but this is not a priority and I'll wait the new many-to-many relationships system before dealing with this in depth.

I created two pages, each containing a specific view with custom search & post relationship filters (FR Regions > FR Cities for the french one, INT Countries > INT Cities for the international one).

Question 1 :
- Finally, I wonder if it would be better to create a unique Cities Post Type from a good practise point of view, but considering the structure (two separate directories) ?

Question 2 :
- Each view displays the whole directory (FR + INT) when the page is loading (when no filter active in the custom search, otherwise it works like a charm as expected).
So how to filter the professional posts according to their ancestors (FR or INT cities) ?
I tested different things in the view Query Filter section but can't figure out how to make it work, could you please help me on this ?

Thanks in advance
Sébastien

PS : the right url for the test site is :
hidden link
look at the menu for the direct link to the FR and INT directories..

#613943

- Finally, I wonder if it would be better to create a unique Cities Post Type from a good practise point of view, but considering the structure (two separate directories) ?
I think it depends on how you want to allow people to search and filter Professionals, and how you want your single posts and archives to be organized into URL directories.

If you use separate CPTs for FR and INT cities, you will have two different URL structures for Cities single posts, like yoursite.com/fr-city/paris and yoursite.com/int-city/london. If you use one single post type, your URLs will be more consistent like yoursite.com/city/paris and yoursite.com/city/london. Similarly your Cities CPT archives will be separated to yoursite.com/fr-city/ and yoursite.com/int-city.

On the other hand, it's not possible for a single Professional to be a child of more than one post in a single post type. This is fine when you have two separate post types for INT and FR Cities, but when all Cities are combined into one post type this could be a problem if a Professional should be a child of both Paris and London.

- Each view displays the whole directory (FR + INT) when the page is loading (when no filter active in the custom search, otherwise it works like a charm as expected).
When you use a hierarchical post relationship filter, you can preselect a hierarchical filter with a URL parameter. You can configure your View to update the URL whenever filters are changed to see how this works. Your site URL will be updated with URL parameters like yoursite.com/search-page/?wpv_view_count=1195&wpv-relationship-filter-grandparent=903&wpv_filter_submit=Submit

So if you want to predefine one of those values (like the grandparent post ID) you can link to the page with the correct URL parameter added to the link, like yoursite.com/search-page/?wpv-relationship-filter-grandparent=903.

Another option is to add some custom code that hides the search results until at least one filter is selected. I have some code I can share if you'd like to explore that option.

#614118

I think it depends on how you want to allow people to search and filter Professionals, and how you want your single posts and archives to be organized into URL directories. [...]
This is fine when you have two separate post types for INT and FR Cities, but when all Cities are combined into one post type this could be a problem if a Professional should be a child of both Paris and London.

Right, I'll keep separate post types for INT and FR Cities, thanks for the clarification !

So if you want to predefine one of those values (like the grandparent post ID) you can link to the page with the correct URL parameter added to the link, like yoursite.com/search-page/?wpv-relationship-filter-grandparent=903.

Ok I think I get it, so for example in the international directory, I'll be obliged to select a single country (say post ID 54 : Belgium) :
hidden link

But does that mean that there's no way to display professionals of all countries except France ?
I mean not based on a single post ID but on a global grandparent reference ?

Another option is to add some custom code that hides the search results until at least one filter is selected. I have some code I can share if you'd like to explore that option.

Oh yes, I'm greatly interested in this option, the code is welcome.

Thank you Christian

#614290

But does that mean that there's no way to display professionals of all countries except France ?
I mean not based on a single post ID but on a global grandparent reference ?

I think what you're describing is possible by selecting a different post relationship filter in two different Views. When creating a hierarchical post relationship filter in a View, you will be presented with different relationship hierarchy options. In the France example, you will filter the View by a post relationship that includes the France ancestor. In the International example, you will filter by a relationship that includes the International ancestor.

I have some code I can share if you'd like to explore that option.
Okay I need to know all the possible URL parameters for these two Views. Please set up the Views so they are showing the expected results first, then select an option in each filter. Copy + paste the generated URLs (one for each View) here for me to review.

#614595

> But does that mean that there's no way to display professionals of all countries except France ?
> I mean not based on a single post ID but on a global grandparent reference ?
I think what you're describing is possible by selecting a different post relationship filter in two different Views.

I may have misunderstood your suggestion, but it is exactly what I did & explained in my first post where I said :
"I created two pages, each containing a specific view with custom search & post relationship filters (FR Regions > FR Cities for the french one, INT Countries > INT Cities for the international one)."

In my first post #613866 I uploaded a screenshot of the view for the french directory,
please check also the link hereafter for the result :
hidden link

Idem for the international directory, where I set this ancestors in the post relationship filter : int-country>int-city
hidden link

The post relationship filters seems to be correctly parametered I think, but each directories keep on displaying the whole directory FR & INT in spite of the separate views with different post relationship filter.

> I have some code I can share if you'd like to explore that option.
Okay I need to know all the possible URL parameters for these two Views. Please set up the Views so they are showing the expected results first, then select an option in each filter. Copy + paste the generated URLs (one for each View) here for me to review.

I'm sorry, my basic english probably, but what do you mean by
Please set up the Views so they are showing the expected results first
The problem is precisely I can't set up the views so they show the expected results..
As for the urls,
for example in the french directory, I selected a region, here is the generated url :
hidden link
in the international directory, I selected a country, the url :
hidden link

I can provide admin credentials if needed.

Thanks

#614994

Please add the following code to your child theme's functions.php file. I think this will help because all results will be hidden until at least one filter is selected:

add_filter( 'wpv_filter_query_post_process', 'drop_empty_search_query_post_process', 10, 3 );
function drop_empty_search_query_post_process( $query, $view_settings, $view_id ) {
    $ids = array(1234,5678);
    if (in_array($view_id, $ids)){
      if (
        // taxonomy filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-relationship-filter-fr-region']) && $_GET['wpv-relationship-filter-fr-region'] != '0' )
          ||
        // taxonomy filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-relationship-filter-int-country']) && $_GET['wpv-relationship-filter-int-country'] != '0' )
      ) {
      } else {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Replace 1234,5678 with a comma-separated list of the View numeric IDs.

#614997

I tested the code and it does the trick.

Thank you Christian!