wp query - Should I reset $wp_query?

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 art

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?

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?

Share Improve this question edited Oct 30, 2019 at 23:28 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Oct 30, 2019 at 23:24 LovinQuaQuaLovinQuaQua 733 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745041849a4607857.html

相关推荐

  • wp query - Should I reset $wp_query?

    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 art

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信