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.

For some reason the WP_Query instance in my shortcode function won't take the query attribute sent from the shortcode. The shortcode is like this: [postlist query="post_type=any&posts_per_page=5" style="list"]

Here's the code.

function hey_query_shortcode( $atts ) {
    extract( shortcode_atts( array(
        'query' => '',
        'style' => ''
    ), $atts ) );

        ob_start(); 
        $the_query = new WP_Query( $query );
        while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<h3><?php the_title(); ?></h3>
    <?php the_excerpt(); ?>
    <h2>I know $style is set bc it shows up here: <?php echo $style; ?></h2>
    <h2>I know $query is set bc it shows up here: <?php echo $query; ?></h2>

<?php
    endwhile; 
    echo '<hr>';
    wp_reset_postdata();

    $list = ob_get_clean();
    return $list;

}

add_shortcode( 'postlist', 'hey_query_shortcode' );

Any idea what is going on here? $query echoes out fine but for some reason it doesn't affect the WP_Query query.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Sorry I deleted my comment because I found the issue. You need to do an html entity decode on query variable. Use the code below.

function hey_query_shortcode( $atts ) {
    extract( shortcode_atts( array(
        'query' => '',
        'style' => ''
    ), $atts ) );

        $query = html_entity_decode( $query );

        ob_start(); 
        $the_query = new WP_Query( $query );
        while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<h3><?php the_title(); ?></h3>
    <?php the_excerpt(); ?>
    <h2>I know $style is set bc it shows up here: <?php echo $style; ?></h2>
    <h2>I know $query is set bc it shows up here: <?php echo $query; ?></h2>

<?php
    endwhile; 
    echo '<hr>';
    wp_reset_postdata();

    $list = ob_get_clean();
    return $list;

}

add_shortcode( 'postlist', 'hey_query_shortcode' );
share|improve this answer
    
Yes, that was it. Thanks. –  supertrue Jun 15 '11 at 4:50

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.