I am running a theme which is using query_post
now I am trying to change it to WP_Query
. However I am always getting a blank empty page with just the header.
This is my query:
query_posts(array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1
));
I am than calling a loop file with get_template_part( 'loop', 'coupon' );
Within this I have if ( have_posts() ) : while ( have_posts() ) : the_post();
.
I changed this to WP_Query
$args = array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1
));
$query = new WP_Query( $args );
and to if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
but I am so stuck...
I am running a theme which is using query_post
now I am trying to change it to WP_Query
. However I am always getting a blank empty page with just the header.
This is my query:
query_posts(array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1
));
I am than calling a loop file with get_template_part( 'loop', 'coupon' );
Within this I have if ( have_posts() ) : while ( have_posts() ) : the_post();
.
I changed this to WP_Query
$args = array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1
));
$query = new WP_Query( $args );
and to if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
but I am so stuck...
The problem here is caused by variable scope...
Everything is fine, when you use global $wp_query
, because it is a global variable and functions like have_posts
and the_post
are aware of that, so you can use them in template parts.
On the other hand, you define your custom query. Then you get template part and you want to use that variable inside that template - but this variable is not accessible in there because it is not a global variable.
That's why your way of dividing code into parts is not so common. It would be much better, easier to maintain and more secure, if you'd write it like so:
$args = array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1
));
$query = new WP_Query( $args );
while ( $query->have_posts() ) :
$query->the_post();
get_template_part( 'loop', 'coupon' ); // or get_template_part( 'content', 'coupon' );
endwhile;
And in loop-coupon.php
should be only the content of single post.
This way you can use the same template part for different loops, so it is easier to re-use.
endwhile;
? Can you post the entire code? – czerspalace Commented Feb 27, 2019 at 20:54endwhile;
and addedwp_reset_postdata();
this is the originalloop
file pastebin/588vHskN – joloshop Commented Feb 27, 2019 at 20:59WP_Query
in that link. Can you post the code you are using and that is causing the error? – czerspalace Commented Feb 27, 2019 at 22:39