Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using WP_Query() to pull out a few posts under a specific category "Featured" to display at the bottom of any post.

So I added <?php if (function_exists('getEditorPicks')) getEditorPicks();?> in single.php.

In functions.php, I have

wp_reset_postdata();
    $args = array('cat' => 4176, 'posts_per_page' => 5);
    $query = new WP_Query($args);
    //return $query->query_vars;
    if ($query->post_count>2) {
        while ($query->have_posts()) {
            $query->the_post;
            echo the_title();
        }
    }
    wp_reset_postdata();

However, it takes forever for the page to load now and it simply exhausts memory in the end.

What am I missing here?

share|improve this question

2 Answers 2

up vote 2 down vote accepted
$query->the_post;

should be:

$query->the_post();

The first is trying to fetch a property named the_post rather than invoking the function so the post never advances, and your loop is infinite.

share|improve this answer

In your codes you did not check if there exists any codes matching your query. So it is advisble to check first if query contains posts - $query->have_posts() - and then proceed.

You may have a look at the code here

share|improve this answer
1  
Do not just post a link as an answer. Please post all relevant code and info from the link, and keep the link only as credit –  Pieter Goosen Jul 13 at 6:12
1  
Strickly speaking, you only need the_post() on single pages. have_posts is not necessary, although it is advisable not to remove it as it may potensionally break plugins that hooks into this function –  Pieter Goosen Jul 13 at 6:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.