0

Basically, I want to query PARENT for a bunch of custom field data and echo it out. Then, I want to query all of the CHILD pages of that parent and echo out more data.

What's the best way to do it?

  1. Top level query for parent then within that query a sublevel a query for the children.
  2. 2 seperate top level queries
  3. Other?

Best to use wp_query or get_posts or combination of both?

UPDATE: Looks like I might have to explain a little better.

If the structure of pages looks like this:

Page Parent ID(2)
 1. Child 1 ID(3)
 2. Child 2 ID(4)
 3. Child 3 ID(5)
 4. Child 4 ID(6)

And the parent query looks like this:

$parent = new wp_query(post_id=2)
while
        //do page parent stuff, eg get the_title(); of the parent page
endwhile

Do I have to run another instance of wp_query to get the child pages, using post_parent=2, or can I somehow re-use the original parent query and extend it with extra parameters to cut down on query calls?

I'm looking for optimisation of the wp_query here when a child query uses the same parameters as the parent plus a couple more. Thanks.

2 Answers 2

1

I don't understand the distinction you are making between 'sublevel' query and 'toplevel' query, but this is how I pull children for a page.

$children = get_children(
    array(
        'post_parent'   => $servid,
        'post_type'     => 'page',
        'post_status'   => 'publish',
        'orderby'       => 'menu_order',
        'order'         => 'ASC'
)); ?>

Or...

$page_children = get_pages(
 array(
  'child_of'    => $post->ID,
  'post_type'   => 'page',
  'sort_column' => 'menu_order',
  'order'       => 'ASC'
 ));

In both cases I am sending the parent post's ID as the first parameter.

http://codex.wordpress.org/Function_Reference/get_children

http://codex.wordpress.org/Function_Reference/get_pages

0
0

Here is an example using categories instead of pages, but you can query the information using the parent's information within the other loop:

// checking current taxonomy to see if it has child categories if it doesn't then we're out of sub categories and should show the current categories products
$args = array(
    'type'       => $taxfunc_post_type,
    'hide_empty' => 1,
    'orderby'    => 'name',
    'order'      => 'ASC',
    'taxonomy'   => $taxfunc_tax_name
);

$categories = get_categories($args);
$categories = wp_list_filter($categories,array('parent'=> $term_id));

foreach($categories as $category) {

    // setup the cateogory ID
    $cat_id = $category->term_id;
    // Get category name
    $cat_name = $category->name;
    // Get category count
    $cat_count = $category->count;
    //get the category url
    $cat_url = get_term_link( $category->slug, $taxfunc_tax_name );

    echo '<div class="cat-block">';
    echo '<header>';
    echo "<h3>";
    echo $cat_name;
    echo "</h3>";
    if ($cat_count > 0) {
        echo '<a href="'.$cat_url.'">';
        printf( _n('1 profile', '%s profiles', $cat_count,'mouldings' ),$cat_count );
        echo '</a>';
    }

    $subcat_args = array(
        'type'       => $taxfunc_post_type,
        'hide_empty' => 1,
        'orderby'    => 'name',
        'child_of'   => $cat_id,
        'order'      => 'ASC',
        'taxonomy'   => $taxfunc_tax_name,
        'pad_counts' => 1
    );
    $subcategories = get_categories($subcat_args);
    $subcat_count = count($subcategories);
    if ($subcat_count > 0) { echo ' <a href="'.$cat_url.'">'; printf( _n('1 subcategory', '%s subcategories', $subcat_count,'mouldings' ),$subcat_count ); echo '</a>'; }
    echo ' <a class="view-all" href="'.$cat_url.'">'.__('View all','mouldings').'</a>';
    echo '</header>';

    echo '</div>';
}

In the above, I'm assigning the child_of param to the parent category. Let me know if that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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