WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is the scenario: I have multiple pages that all have a custom field named "Location" Those pages all have child-pages with the custom field of "size_m2".

What I want is to do a normal meta_query to list all pages with let say "size_m2 >= 55", but only if the parent page have "Location == Stockholm".

Do i need to use a custom SQL query for this? Or can I query for the parent_pages children, and when filter and "query" that query?

share|improve this question
I would edit this Question to adjust grammar and spelling, but I don't understand: what is och? You can edit the Question whenever needed. English language errors are no problem, we can correct them, but if you use a Spell Checker in your browser, helps a lot ;) – brasofilo Apr 9 '13 at 15:32
@brasofilo "och" means "and" in Swedish, apparently. – vancoder Apr 9 '13 at 16:40
you should use WP_Query, do not use raw sql queries – Tom J Nowell Apr 9 '13 at 16:46
up vote 0 down vote accepted

Your problem is you have:

foreach post where A is true, that has a child where B is true

Which is actually:

foreach post where A is true
    that has a child where B is true

So this is not something you should be doing in 1 query. Instead you would need to do n queries where n= parentposts+1. 1 query to find the parents, and then a query for each parent to find the children.

Clearly this is not going to be fast.

The Root Of The Problem

Your data structure is inappropriate. You've attempted to do is reinvent taxonomies using post meta.

Instead, create a custom taxonomy called 'Location', and create a Stockholm term. Then do a query for all posts that have the meta for size, and add 'Location' => 'Stockholm'.


  • Querying is dramatically simplified
  • You're given a working, extensively tested admin interface for free
  • Its fast
  • You get free archives, e.g. example.com/location/stockholm/
  • Tight integration into the template heirarchy with new templates: taxonomy.php, taxonomy-location.php and even taxonomy-location-stockholm.php


  • None
  • Nothing
  • There aren't any

As a sidenote, if you run into a situation where you need to execute an sql query, you've done something wrong.

share|improve this answer
The problem is that cause of limitations to my setup i cant place the child pages in a texonomy, can I query for all post in a taxonomy, including the children, and then filter by meta? – Robin Westerlundh Apr 9 '13 at 18:15
taxonomies can be applied to pages too, and there's nothing stopping you writing a snippet of code to run once that creates and assigns appropriate terms. I cannot think of a single instance you would not be able to use taxonomies that allows you to be able to modify the code. – Tom J Nowell Apr 9 '13 at 19:33
And yes, you can query all posts in a taxonomy. I suspect your understanding of what a taxonomy is, is incomplete. e.g. a category is a taxonomy, the 'wordpress' category is a term in the category taxonomy. Tags are a taxonomy, a tag is a term in the tags taxonomy. – Tom J Nowell Apr 9 '13 at 19:34
I think you misunderstood me, I have a heavily modified WordPress admin setup where i have divided pages with a custom tax, to make what I call "subposttypes" So that i can separate content, just like Custom Posttypes, but they all share the root slug. So I cant share the location tax with the children, it will mess stuff up, but thanks for taking time and helping me out! – Robin Westerlundh Apr 10 '13 at 22:42
So basically you're unaware of the rewrite option when registering taxonomies and post types, and implemented a complicated system of 'sub post types' that attempts to recreate it? – Tom J Nowell Apr 10 '13 at 22:47

You cannot run a subquery on a post's parent using WP_Query, unfortunately. You could of course run 2 separate WP_Queries, using PHP to apply the results of the first (on the parent) to the WHERE of the second (the child).

However, I would just do a custom query using wpdb.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.