Home > database >  wp_query in shortcode not working as expected
wp_query in shortcode not working as expected

Time:07-25

I created a simple shortcode

add_shortcode('lichthidau', 'hp_lich_thi_dau');
function hp_lich_thi_dau( $atts ) {

$output = '';
extract( shortcode_atts( array( 'posttype' => 'lich' ), $atts) );
$args = array( 
    'post_type' => $posttype,
    'post_status' => 'publish',
    'posts_per_page' => -1, 
    'orderby' => 'title',
    'order' => 'ASC',
 );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
    $itemprop = '';

    if ( 'microdata' === generate_get_schema_type() ) {
        $itemprop = ' itemprop="text"';
    }

    echo '<div >';
        
        //while ( $the_query->have_posts() ) {
            $output = $the_query->found_posts;
        //}
    echo '</div>';
}
wp_reset_postdata();

return $output;

}

Then put it in Gutenberg shortcode block [lichthidau] in a page (ID = 106, for example). Without while loop, it's showing 2, which is the count of returning posts, and it's correct. However, if I enable while loop, it's taking the current page ID (106), and creating unlimited loops, while the expected result should be only two number 2.

Can anyone advice why and how to fix, please? Thanks.

CodePudding user response:

The first problem is that you're using echo in the shortcode output. The shortcode can only return content, and echo will produce unexpected results.

The second problem is trying to output the $output = $the_query->found_posts; within your loop. If you return something else, it will work.

This returns your loop with the post titles.

add_shortcode( 'lichthidau', 'hp_lich_thi_dau' );
function hp_lich_thi_dau( $atts ) {

    $output = '';
    extract( shortcode_atts( array( 'posttype' => 'lich' ), $atts ) );
    $args      = array(
        'post_type'      => $posttype,
        'post_status'    => 'publish',
        'posts_per_page' => -1,
        'orderby'        => 'title',
        'order'          => 'ASC',
    );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
        $itemprop = '';

        if ( 'microdata' === generate_get_schema_type() ) {
            $itemprop = ' itemprop="text"';
        }

        $output = '<div >';

        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $output .= get_the_title( get_the_ID() ) . '</br>';
        }
        $output .= '</div>';
    }
    wp_reset_postdata();

    return $output;
}
  • Related