0

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?

1
  • 1
    What's the full error? You've removed all the useful parts of the error message, such as the line it happened on and the file. Why do you think that this post loop is the cause? There is nothing in the questions code capable of producing these errors
    – Tom J Nowell
    Commented Mar 25, 2021 at 13:40

2 Answers 2

2

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.

-1

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.

2
  • There's no reason to assume the user has not registered a post type named 'catalog'. Even if they hadn't , this is not the error they would be seeing. Lastly, your code uses query_post(), which is extremely poor practice. There's even a big warning in the documentation: developer.wordpress.org/reference/functions/query_posts Commented Mar 25, 2021 at 13:40
  • query_posts is dangerous, and causes lots of problems, it should always be avoided.
    – Tom J Nowell
    Commented Mar 25, 2021 at 13:40

Your Answer

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

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