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 badges1 Answer
Reset to default 1The 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
评论列表(0条)