C
C
Casey2021-10-26 20:17:11
WordPress
Casey, 2021-10-26 20:17:11

Why is the cycle not working in WordPress?

I'm accessing the WP_Query class to get all posts from the "category" taxonomy with the term "avto".

$postsList = new WP_Query( array(
          'post_type' => 'post',
          'posts_per_page' => 10,
          'tax_query' => array(
            array(
              'taxonomy' => 'category',
              'field'    => 'slug',
               'terms'    => 'avto'
              )
            )
          ) );


The $postsList variable returns an object with the data I need.

Now I want to make a loop to display titles and posts.

if( $postsList->have_posts() ) {
            while( $postsList->have_posts() ){
              the_post();
              the_title();

            }
          }
          wp_reset_postdata();

But nothing works. after refreshing the page, the site takes a long time to load and then just crashes.
$postsList->have_posts() - returns true
$postsList - returns an object with posts

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kryamk, 2021-10-26
@malevi

$postsList->the_post();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question