wp query - trying to change from query_post to WP_Query

admin2025-06-02  1

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...

Share Improve this question edited Feb 27, 2019 at 22:55 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 27, 2019 at 20:30 joloshopjoloshop 211 silver badge8 bronze badges 4
  • Do you also have an endwhile;? Can you post the entire code? – czerspalace Commented Feb 27, 2019 at 20:54
  • Yes I do habe endwhile; and added wp_reset_postdata(); this is the original loop file pastebin/588vHskN – joloshop Commented Feb 27, 2019 at 20:59
  • I don't see WP_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
  • I think I found the problem, will test a little and if there is still a problem get back to you. – joloshop Commented Feb 28, 2019 at 14:57
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748856570a314291.html

最新回复(0)