1

This code goes into an infinite loop! What am i doing wrong? There is only one post called “hello there”. The only way to stop it is use break; in while.

Any help appreciated

   $gotop="hello there";

$args = array(

    's' => $gotop,
    'post_type' => 'post'

);

$wp_query = new WP_Query($args);

if ( $wp_query->have_posts() ) :  ?>

        <?php

        while ( $wp_query->have_posts() ) {
      $wp_query->the_post();
}

else:
  echo "nothing found.";
endif;
?>

1 Answer 1

2

You are using $wp_query which is global query variable for wordpress so everytime its checking for new post.

Instead of $wp_query use other variable or use below code.

$gotop="hello there";

$args = array(

    's' => $gotop,
    'post_type' => 'post'

);

$custom_query = new WP_Query($args);

if ( $custom_query->have_posts() ) :  ?>

        <?php

        while ( $custom_query->have_posts() ) {
      $custom_query->the_post();
}

else:
  echo "nothing found.";
endif;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

the $wp_query was the issue thanks. i renamed it and worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.