0

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

  • Do you also have an endwhile;? Can you post the entire code? – czerspalace Feb 27 at 20:54
  • Yes I do habe endwhile; and added wp_reset_postdata(); this is the original loop file pastebin.com/588vHskN – joloshop Feb 27 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 Feb 27 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 Feb 28 at 14:57
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.

  • Hi I use your code, however it looks like the side I trying to load but is stuck in the loop. This is the newest file: pastebin.com/QVTaqDhY and this is the loop file: pastebin.com/jy6SUwPM – joloshop Feb 28 at 13:58
  • I think I found the problem, will test a little and if there is still a problem get back to you. – joloshop Feb 28 at 14:57

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.