WordPress query undefined offset in loop

I created a query with arguments see blow.
On the page I see an error in a loop

  • Notice undefined offset 1
  • Notice undefined offset 2
  • Notice undefined offset 3 and so on…
    $args = array (
      'post_type'     => 'catalog',
      'post_status'   => 'publish',
    );
    $loop = new WP_Query( $args );
       if ( $loop->have_posts() ) {
          while ( $loop->have_posts() ) {
              the_post(); 
              echo get_the_title();
          }
      }

I tried other arguments but this does not work.

  • ‘posts_per_page’ => 4

Please who can help me?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There might be other causes to the issue in question, but one issue I noticed in your code is that because you are looping through a custom WP_Query instance, i.e. $loop, then you need to use $loop->the_post() instead of just the_post():

while ( $loop->have_posts() ) {
    $loop->the_post();
    the_title();
}

And you could see that I simply called the_title() and not doing echo get_the_title(), so you should also do the same.

Method 2

There are five default Post Types readily available to users or internally used by the WordPress installation:

  1. Post (Post Type: ‘post’)
  2. Page (Post Type: ‘page’)
  3. Attachment (Post Type: ‘attachment’)
  4. Revision (Post Type: ‘revision’)
  5. Navigation menu (Post Type: ‘nav_menu_item’)

refer this link for more: https://developer.wordpress.org/themes/basics/post-types/
according to me your post type in the query is wrong.
use post_type => 'post'
instead of post_type => 'catalog'

$args = array (
  'post_type'     => 'post',
  'post_status'   => 'publish',
);
query_posts($args); 
                if(have_posts()) {
                    while(have_posts()) {
                        the_post();
                        echo get_the_title();
                           }
                     }

Please correct it if you find it correct.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x