Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
           );
      wp_reset_query();
      $i=0;
      $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.'">';
        if(has_post_thumbnail()):;
        $output .= '<a href="'.
                    get_permalink().
                    '" a>'.
                    get_the_post_thumbnail(get_the_ID(), 'blog-post-carousel').
                    '</a>';
        else :;
        $output .= '<a href="'.
                    get_permalink().
                    '" a>'.
                      '<img src="'.
                       get_bloginfo( "template_url" ).
                       '/images/theme/no-image.png" width=115 height=115> </a>';

        endif;
        $output .=

               '<h4>'.
               get_the_title().
               '</h4>'.
               '<p>'.
               get_the_excerpt().
               '</p>'.
               '</div>';

      }
      wp_reset_query();
      $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"]

share|improve this question
1  
How are you calling the shortcode? –  Rutwick Gangurde Dec 20 '11 at 18:30
1  
I omited that part sorry, something like [scname parent="5"], when multiple values I do, parent="5,10,15" –  andresmijares25 Dec 20 '11 at 18:36
1  
Check @EAManns answer... –  Rutwick Gangurde Dec 20 '11 at 19:10

1 Answer 1

up vote 4 down vote accepted

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 ...

share|improve this answer
    
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 –  andresmijares25 Dec 20 '11 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 Dec 20 '11 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! –  andresmijares25 Dec 21 '11 at 1:58

Your Answer

 
discard

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.