Here is my code
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page','post_parent'=>$parid,'orderby'=>'title','order'=>'ASC' ));
It displays the first level sub pages only. I need all the sub page, sub’s sub page … and all. I searched for a solution and i can get all sub pages using get_pages and wp_list_pages.
But i really need to sort the order by custom post meta value. So i have to use custom query.
please help. Thanks
Why not just use
get_pages()
?e.g.
But if it really must be as a
WP_Query()
object, use a similar method:The Problem
What you’re having problems grasping is “How do I do X?” This isn’t a 1 step action, it’s a multistep process, and it needs to be broken apart.
You don’t need to do this:
You need to do this:
The General Solution
So, to understand how to infinitely do it until you reach the end, without hardcoding it, you need to understand recursive functions.
e.g.
Applying Recursion To This Problem For a Solution
So your parent is
$parid
, and your post meta has a key of$metakey
.Lets pass it into a function to grab its children.
Then we’ll sort the $children array, the keys will be the post IDs, and the values will be the meta values.
and lets define the function as:
This gives you an array of post IDs and values, ordered from lowest to highest. You can use other PHP sorting functions to do it from highest to lowest.
Now What About the Childrens Children?
In the middle of our loop, we need to make a recursive call, passing in the child rather than the parent ID.
So this:
Becomes this:
With this modification the function now retrieves the children, the childrens children, the childrens childrens children….. etc
At the end you can trim off the values on the array to get IDs like this:
Using this strategy you can replace the meta key value with any other metric, or use recursive functions in other ways.
Since the full code requires only a few seconds of basic comprehension and a quick copy paste, I shan’t insult your intelligence with a full copy paste block of code.
Advantages
Problems You Will Encounter
My Recommendation
I would recommend you either flatten your page hierarchy or use a taxonomy instead. E.g. if you’re rating posts, have a Page Rating taxonomy with the terms 1,2,3,4 and 5 etc. This will provide you with a post listing out of the box.
Alternatively, use nav menus and bypass this problem entirely
I’ve made a recursive function that gets all the children ids of a parent page. After we have the ids, we make query for the pages and can order the results by meta key/value.
If you need to sort the children by meta key/value in a hierarchical manner, you should pass the meta_key and order_by values to the WP_Query in the _get_children_ids function (instead of the final WP_Query).
If not, a simpler method to get all the child id is:
Recursively get all current sub-pages
Here is a recursive approach using
get_children
. Put the following in yourfunctions.php
:How to use it
Use the above function wherever you want, for instance like this:
The function does support an
args
parameter (query string or array) and anoutput
type (see above).So you could also use it like so:
And due to the dependency
get_children
=>get_posts
=>WP_Query
you can use meta values, as initially requested by the author of this question.Not sure if this is exactly what you are after, but you could use the wp_list_pages function, and use the ‘child_of’ and ‘depth’ parameters.
See the following page on the Codex for more info:
http://codex.wordpress.org/Function_Reference/wp_list_pages
I MAKE THIS WORKING, JUST COPY PASTE THE CODE TO YOUR PAGE.PHP FILE