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

I am doing the following to get all the children associated with a post:

    $args = array(
        'order'=> 'ASC',
        'post_parent' => get_the_ID(),
        'post_status' => 'publish',
        'post_type' => 'task'
    $tasks = get_children( $args );

Now the posts of type task have postmeta associated with them. One particular meta_key is task_progress. I would like to get all the children whose task_progress meta_value is less than 100. What is the best way to filter the results?

share|improve this question
up vote 0 down vote accepted

This should do the trick:

global $wpdb;
$parent = 1; // Make sure it's int (intval() it) as it's not escaped in the query
$posts = $wpdb->get_results(
    "SELECT `post_id` AS `ID`, `meta_value` AS `Progress` FROM {$wpdb->postmeta}
        WHERE `meta_key`='task_progress' AND `meta_value` < 100 AND `ID` IN
            (SELECT `post_id` FROM {$wpdb->posts} WHERE `post_parent`={$parent})
        ORDER BY `meta_value` DESC, `post_id` DESC;" // Sort descending
foreach($posts as $post){
    $progress = $post->Progress;
    $post = get_post($ID = $post->ID);
    // Print here


share|improve this answer
Is there a way to do this without a direct query? – v0idless Oct 22 '11 at 3:17
Not sure and, if it were, there's too much overhead in the parametrized WP queries. This returns just IDs and Progress. Light data, kind on resources. Usually WP returns Post Objects, very inefficient. – EarnestoDev Oct 22 '11 at 3:26

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.