By Neelam Khan


2019-02-06 10:05:13 8 Comments

I am trying to display a list of all child pages using wp_query and so far I have created a function that retrieves all the child pages for the current page, however I need to sort these pages into 2 lists by the custom field.

The custom field is called type and it is a select field with the following values:

product and about I need to split the list of pages into 2 groups based on the field value and I can't seem to group them by the custom field.

I need the output to be:

Product
- Page 1
- Page 2
- Page 3

About
- Page 1
- Page 2
- Page 3

So far I have a list of all the pages with no way of separating them and adding the value of the custom field in between.

function get_child_pages($page_id, $post_type = 'page')
{
    // Set up the objects needed
    $custom_wp_query = new WP_Query();
    $all_wp_pages = $custom_wp_query->query(array('post_type' => $post_type, 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'type', 'value' => 'Product',));

    // Filter through all pages and find specified page's children
    $page_children = get_page_children($page_id, $all_wp_pages);

    return $page_children;
}

foreach (get_child_pages(wp_get_post_parent_id(get_the_ID())) as $s) {
  echo '<li><a href="' . get_the_permalink($s->ID) . '">' . get_the_title($s->ID) . '</a></li>';

}

1 comments

@tmdesigned 2019-02-06 10:30:27

You're close. You have:

foreach (get_child_pages(wp_get_post_parent_id(get_the_ID())) as $s) {
  echo '<li><a href="' . get_the_permalink($s->ID) . '">' . get_the_title($s->ID) . '</a></li>';
}

We need to add a layer above that to show the two lists. However we can't assume that the posts of one type will all come out together (i.e. all of one category, then all of the next). That means we can't just spit it all out in the order it comes in.

Here's a way utilizing a holding object to first group the posts by their value of that field:

//this will hold the post IDs sorted by outer groups
$list_groups = array(); 

foreach (get_child_pages(wp_get_post_parent_id(get_the_ID())) as $s) {

    //get the category of this post
    $list_category = get_post_meta( $s->ID, "type", true );

    //if this is the first post of that category, initialize the array
    if ( ! isset( $list_groups[ $list_category ] ) ){
        $list_groups[ $list_category ] = array();
    }

    //add this post ID to that category in our holding array
    $list_groups[ $list_category ][] = $s->ID;

}

//at this point we have an array ($list_groups) that has an inner array of post IDs for each category

foreach ( $list_groups as $category_name => $category_posts ){

    echo "<div class='category-group'><h2>$category_name</h2><ul>";

    foreach ( $category_posts as $category_post_id ){

        $p_permalink = get_the_permalink( $category_post_id );
        $p_title = get_the_title( $category_post_id );

        echo "<li><a href='$p_permalink'>$p_title</a></li>";

    }

    echo "</ul></div>";

}

So what we've done is first created a holding array that ends up looking like this:

$list_groups
---Product
   ---10
   ---15
   ---33
---About
   ---5
   ---12
   ---55

It's just an array holding arrays for each type, and in those arrays are the IDs of the posts that belong there.

Then we loop through that holding array and spit them out by category.

Sidenote: your wp_query looks to be limiting the results to just one of the two categories. I could be misreading that but if you only get one category out check it.

@Neelam Khan 2019-02-06 10:35:39

This did the trick! Thank you @the_hobbes how do I display the label of the "type" field as opposed to the value?

@tmdesigned 2019-02-06 13:48:24

Meta data has a key and a value. Right now this snippet shows the value. The key would just be "type". I will take a guess that what you mean is that when you set the meta data you have a select box with options like <option value="VAL">TEXT</option> and here you want to show TEXT instead of VAL. Unfortunately you are only saving VAL to the database. You would have to write a function or other way to convert your VALs back to the TEXTs (or better, have both the dropdown and this output pull the VAL & TEXT pairs from a common place).

Related Questions

Sponsored Content

1 Answered Questions

wp_query posts with custom fields number smaller than

1 Answered Questions

How add a custom posttype name using ACF field to a query post array

1 Answered Questions

[SOLVED] Add custom field to all posts in specific post_type

0 Answered Questions

WP_Query (list) posts according to author's custom field

  • 2016-04-22 11:35:21
  • Pete
  • 309 View
  • 0 Score
  • 0 Answer
  • Tags:   wp-query author

1 Answered Questions

[SOLVED] Query_posts with custom field meta value

1 Answered Questions

[SOLVED] Display custom field & value correctly on page

  • 2013-08-01 12:04:45
  • Ajj
  • 2708 View
  • 0 Score
  • 1 Answer
  • Tags:   custom-field

1 Answered Questions

[SOLVED] Pagination works in custom query loop but it doesn't work in shortcode

  • 2013-10-31 03:52:45
  • elenakoroleva
  • 1079 View
  • 0 Score
  • 1 Answer
  • Tags:   wp-query pagination

1 Answered Questions

Group pages by custom field values

2 Answered Questions

[SOLVED] Orderby custom field meta value ASC and then by date DESC

2 Answered Questions

[SOLVED] Filter or order based on custom field

Sponsored Content