I need to pass a few values into the parent attribute of the wp_query. However I'm using a shortcode, so, If I pass a single value works perfect, but if I need to send multiple values it breaks, would be a great holiday gift if some1 could help me with it.

By default, it works with the current post ID $thePostID, however If I need to retrieve the parent I'm using the parent attribute. Take a look at my code.

 function andrew_child_loop_shortcode_new ( $atts ) {
      global $post; $thePostID = $post->ID;
      extract ( shortcode_atts (array (
        'posts' => 20,
        'parent' => $thePostID,
        'exclude' => array(0)
      ), $atts ) );
      ///////$atts[ 'parent' ] = explode( "," $atts[ 'parent' ] );
      ///////extract( $atts );
      $output = '<div class="clear"></div>';
         $args = array(
            'orderby' =>  'menu_order',
            'order' => 'ASC',
            'post_parent' => $parent,
            'post_type' => 'page',
            'post__not_in' =>  array($exclude),
            'posts_per_page' => $posts
      $andrew3_query = new  WP_Query( $args );
        $output .= '<div id="multiple-childs" class="grid_8">';
      while (  $andrew3_query->have_posts()) { $i++ ; $andrew3_query->the_post();
        $output .= '<div id="child-results-multiple" class="grid_2 omega result-'.$i.'">';
        $output .= '<a href="'.
                    '" a>'.
                    get_the_post_thumbnail(get_the_ID(), 'blog-post-carousel').
        else :;
        $output .= '<a href="'.
                    '" a>'.
                      '<img src="'.
                       get_bloginfo( "template_url" ).
                       '/images/theme/no-image.png" width=115 height=115> </a>';

        $output .=


      $output .= '</div>';
      return $output;
    add_shortcode('display_childs_multiple', 'andrew_child_loop_shortcode_new');  

I'm calling the parent this way [display_childs_multiple parent="10,15,20"]

  • 1
    How are you calling the shortcode? Commented Dec 20, 2011 at 18:30
  • 1
    I omited that part sorry, something like [scname parent="5"], when multiple values I do, parent="5,10,15" Commented Dec 20, 2011 at 18:36
  • 1
    Check @EAManns answer... Commented Dec 20, 2011 at 19:10

1 Answer 1


If you're passing in a comma-delimited list of parent IDs when you call the shortcode, it's pretty easy to turn this to an array.

For example, using [scname parent="5,10,15"]:

function andrew_child_loop_shortcode_new ( $atts ) {
    global $post; $thePostID = $post->ID;

    $atts = shortcode_atts( array(
        'posts'  => 20,
        'parent' => $thePostID
        ), $atts );

    // Turn the 'parent' parameter into an array
    $atts[ 'parent' ] = explode( ",", $atts[ 'parent' ] );

    extract( $atts );

    // ... now do whatever you were going to do ...


Now your $parent variable is an array of the post IDs passed in. If only one was passed in, it's array with a single element. If you passed in multiple IDs, then you'll have an array with multiple values.

How you construct your queries and move on from here is entirely up to you ...

  • Hi, I appreciated your help, however for some reason is not working :(, I first used your code exactly as it is, and got unexpected T_VARIABLE, then I realized that probably u missed a , in the explode function ("," , $atts['parent']), after I did not work neither, then I check the way I'm calling the post_parent in the $args, I tried with post_parent => $parent, and also post_parent => array($parent), and it keeps displaying all pages literally, like query is not retrieving anything, I will update the full code so maybe you can help me better, thanks a ton Commented Dec 20, 2011 at 19:14
  • Thanks for catching the typo, it's fixed. As for post_parent ... you have to use an int for this, you can't put in an array. Just a single post ID. Double check the docs on WP_Query and you'll see what I mean.
    – EAMann
    Commented Dec 20, 2011 at 21:07
  • 1
    I try passing the parameter as an Int however it only takes one value, looks like I'm not able to use multiples parents in my query, however your solution work for the porpouse. If you have any idea about how to pass multiples values, I would appreciated ur help, regards! Commented Dec 21, 2011 at 1:58

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.