Skip Navigation

[Resolved] Filter pages of grandparents

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

Problem: I have a View that shows Pages (a hierarchical post type). I would like to show a random Page, filtered by a post ancestor. Any descendant Pages of that ancestor should be shown by the View.

Solution: Use a custom shortcode to generate a comma-separated list of all the descendant post IDs:

function get_all_descendent_pages($atts) {
  global $post;
  $all_wp_pages = get_pages();
  $descendent_pages = get_page_children( $post->ID, $all_wp_pages );
  $ids = array();
  foreach( $descendent_pages as $dp ) {
    array_push( $ids, $dp->ID );
  }
  return implode(',', $ids);
}
add_shortcode("get-all-descendent-pages", "get_all_descendent_pages");

Place this shortcode on your ancestor Page:

[get-all-descendent-pages] //results: 123,456,789

Add this shortcode in Toolset > Settings > Front-end Content > Third-party shortcode arguments.

Use the custom shortcode to supply the value of a post IDs shortcode attribute for your View:

[wpv-view name="anderen-lezen-ook-nz" ids="[get-all-descendent-pages]"]

Change the View's Query Filter to filter by post ID, set by a shortcode attribute "ids".

This support ticket is created 6 years, 3 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 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1087066

Tell us what you are trying to do?

I have a main-page with several sub-pages. These sub-pages have sub-pages as well.
I am trying to make a view where some pages, subpages and subsubpages are randomly shown.

I tried it with following shortcode: {!{wpv-view name="anderen-lezen-ook-nz" wpvchildof="9950, 10024, 9970, 9978"}!} but that only returns subpages of the first ID.

Is there any documentation that you are following?

I read this post so I know it want be easy 😉

Is there a similar example that we can see?

What is the link to your site?

hidden link Just above the footer

#1087126

Hi, since you know the parent Page IDs, I would use two Views.
View 1. View of Pages filtered by post ID, where the ID is set by one shortcode attribute "ids".
View 2. View of Pages filtered by post parent, where the parent is the current post in the Loop.

In View 2, include the post title with link in the Loop editor.
In View 1, place View 2 in the Loop editor.

#1088347

Hello Christian,

Thanks for your answer, but unfortunately it is not exactly what I mean. I try to clarify my question.

We have the following page hierarchy:

Portugal
- History
-- Ancient Portugal
-- Medieval history
--- Crusaders in Tomar
--- Battle near Porto

My goal is to make a view where visitors randomly see (through title and featured image) 4 item who's parent, grand parent or even great grand parent is Portugal.

#1088475

Okay thank you. There isn't a post hierarchy filter exactly like what you need, so the best alternative is a post ID filter. Remove the post parent filter and replace it with a post ID filter set by a shortcode attribute "ids". You will pass in the IDs of all the pages in the hierarchy as a shortcode attribute, and the View will randomly select from those IDs. This means you need to know the IDs of all the ancestor Pages in this hierarchy. I can give you a custom shortcode that will help with this. Add the following code to your child theme's functions.php file:

function get_all_descendent_pages($atts) {
  global $post;
  $all_wp_pages = get_pages();
  $descendent_pages = get_page_children( $post->ID, $all_wp_pages );
  $ids = array();
  foreach( $descendent_pages as $dp ) {
    array_push( $ids, $dp->ID );
  }
  return implode(',', $ids);
}
add_shortcode("get-all-descendent-pages", "get_all_descendent_pages");

Place this shortcode on the Portugal page, and it should return a comma-separated list of post IDs like this:

[get-all-descendent-pages] //results: 123,456,789

These represent the IDs of all the descendent Pages of the Portugal Page.

Go to Toolset > Settings > Frontend Content and scroll to the "Third-party shortcode arguments" section. Add get-all-descendent-pages here. Now you can use this custom shortcode in the attribute of another shortcode - the View shortcode:

[wpv-view name="anderen-lezen-ook-nz" ids="[get-all-descendent-pages]"]

If this View is not placed on the Portugal Page, we need to make some adjustments so you can pass some arbitrary Page ancestor ID into the View.