0

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.

0

1 Answer 1

1

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' );
0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.