Posts can be directly related to each other with a relationship or they can be related through other posts. You can create custom searches that allow visitors to filter and display posts that are related by their ancestors.

Here, we will use an example of “Houses” located in “Neighborhoods” of “Cities” that are located in “Countries”.

Filter and display posts that are related by their ancestors

The following image displays the custom search we want to create.

Search results on the front end
Search results on the front end

Naturally, you start by creating a View that lists the needed post type. In our example, it’s the “Houses” post type.

Then, click to add a new filter in the Search and Pagination section and select the Filter by post relationship / repeatable field groups owner. In the dialog that appears, you can add ancestors that connect your posts. This allows you to filter out results based on a relationship between your posts. Depending on a number of relationships “between” your post types, you can add one or more ancestors by clicking the Add new ancestor button.

Adding new ancestor
Adding new ancestor

In this example, Cities and Countries are in a relationship. When you click on Insert shortcode button you will get a code similar to the one displayed in the picture below.

Shortcode at Search and pagination section
Shortcode at Search and pagination section

On the front-end, visitors will now see dropdown menus to filter displayed houses by Neighborhood, City, and Country.

Front end filter