0

I am trying to build wp_query loop with custom post type and it seems I get the infinite loop as the result. Here is my code that I am using:

$apartargs = array (
    'post_type' => 'apartmani',
    'order'     => 'ASC',
    'orderby'   => 'name',
    'post_per_page' => 3
);

$apartquery = new WP_Query( $apartargs );

if ( $apartquery->have_posts()) :
    while ( $apartquery->have_posts() ) : $apartquery->have_post(); ?>
        <h2 class="naslov"><?php echo get_the_title(); ?></h2>
        <span class="vrijeme"><?php echo get_the_date(); ?></span>
        <h3><?php echo the_field( 'cijena_po_danu' ); ?></h3>
        <h3><?php echo the_field( 'kvardratura' ); ?></h3><?php
    endwhile;
else : ?>
    <p class="stadrugoreci">Nema apartmana za tebe</p><?php
endif;

Also note that this is page template that should display list of this custom post types.

Thank you in advance.

  • $apartquery -> have_post(); should really be $apartquery->the_post();. – Luis Sanz May 1 '16 at 16:20
  • I wasn't close to notice that xD Thank you, I fixed it but I still have infinite loop :/ – Usce May 1 '16 at 16:23
  • 1
    please update the question with your fixed code – bravokeyl May 1 '16 at 16:32
  • @bravokeyl I just replaced, and I still get infinite loop – Usce May 1 '16 at 16:46
  • Did you replace as @LuisSanz mentioned above ? – bravokeyl May 1 '16 at 16:49
0

The first thing I would recommend is verify the number of posts your query is returning using the following:

$apartquery->found_posts

| improve this answer | |
0

You have mistake in your code:

Wrong Code

{ while ( $apartquery->have_posts() ) : $apartquery->have_post() }

Right Code

{ while ( $apartquery->have_posts() ) : $apartquery->the_post() }

| improve this answer | |
0

You might have forgotten wp_reset_postdata() after the while statement as well:

    apartquery = new WP_Query($apartargs);

    if ($apartquery->have_posts()) :
        while ($apartquery->have_posts()) :
            $apartquery->the_post();
            // Your content

        endwhile;
        wp_reset_postdata();
    else :
        // Your content
    endif;
| improve this answer | |

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.