0

I am not totally new on WordPress but the query thing is a bit confusing to me. I searched the web and read a lot of articles, but I couldn't find what I needed. Maybe it could help a lot of people who have WordPress skills like me.

I want to create a shortcode and I need to loop through a custom post type called events.

function leweb_get_suggested_events() {

    // Query
    global $wp_query;

    // Arguments  
    $args = array(
        'post_type' => 'events',
    );  

    ob_start();

    $query = new WP_Query( $args );

    if( $query->have_posts() ) {
        echo '<ul>';
        while( $query->have_posts() ) {
            $query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    }

    $output = ob_get_clean();
    return $output;

}
add_shortcode( 'leweb_suggested_events', 'leweb_get_suggested_events' );

The query is right and its working. But should I reset something?

I call the global $wp_query to get in the main loop. This is what I found out on reading some articles.

Then I start my own loop with $query = new WP_Query($args); So I have two loops right? The main loop and my own loop.

Should I reset them both or just one of them, or is it fine like above?

1

The query is right and its working. But should I reset something?

Yes!

Should I reset them both or just one of them, or is it fine like above?

Neither!

Your WP_Query object is self contained in $query, so there's no reason to touch the $wp_query global variable, why would you? $wp_query represents the main query, so leave it alone.

What you need to reset, is the current post. Notice $query->the_post() sets the current post, you need to undo that after the loop ends:

    if( $query->have_posts() ) {
        while( $query->have_posts() ) {
            $query->the_post();
            ....
        }
        wp_reset_postdata();
    }

A Word on wp_reset_query()

The wp_reset_query() function is something you might see in your research, but avoid this. It's intended to clean up after query_posts, which is a function you should never use.

If you ever find yourself needing to use query_posts, or wp_reset_query in your client work, something has gone terribly wrong. Scrub these from your memory, scream when you see them in code, and refactor them with extreme predjudice.

Shortcodes and Blocks

Have you considered turning this into a GB block instead of a shortcode? Just create a block that's rendered in PHP, there's no need to learn React or advanced JS

  • Thank you Tom for this perfect answer!!! What do you mean with GB block? – LovinQuaQua Oct 31 at 0:05
  • A block in the new block editor that got added in WP 5.0 – Tom J Nowell Oct 31 at 13:06
  • Do you have any serious source, where i can read about it? – LovinQuaQua Oct 31 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.