1

I have errors in WordPress:

PHP Notice: Undefined offset: 0 in /home/userpro/public_html/wp-content/themes/hoon/inc/tweaks.php on line 602

PHP Notice: Trying to get property of non-object in /home/userpro/public_html/wp-content/themes/hoon/inc/tweaks.php on line 602

Code:

/**
 * Display Future Posts
 *
 * Display future post in the events category to all users.
 */
function hoon_show_all_future_posts( $posts ) {

    global $wp_query, $wpdb;

    if ( is_single() && $wp_query->post_count == 0 ) {
        $events_cat = hoon_option( 'events_category' );
        $request = $wpdb->get_results( $wp_query->request );
        /* Line 602 bellow  */  
        if ( post_is_in_descendant_category( $events_cat, $request[0]->ID ) || in_category( $events_cat, $request[0]->ID ) ) {
            $posts = $request;
        }
    }
    return $posts;
}
add_filter( 'the_posts', 'hoon_show_all_future_posts' );
1
  • This should be done using pre_get_posts, you shouldn't use the_posts to change a query like this
    – Tom J Nowell
    Commented Jan 2, 2021 at 0:33

2 Answers 2

1

If $wp_query->post_count == 0 I cant see how $wpdb->get_results( $wp_query->request ) would return any posts. So basically

$request = $wpdb->get_results( $wp_query->request );

contains nothing and $request[0] doesn't exist. Ergo

PHP Notice: Undefined offset: 0

It would be interesting to know, what this could is supposed to achieve.

0

The first error message is for undefined variable which you used $request[0] and second one is for $request is not a object so you cannot use as object.

I suggest you that use below code and check what you missed:

$request = $wpdb->get_results( $wp_query->request );
print('<pre>');
print_r($request);
print('</pre>'); exit;

Above code shows what $request contains.

Let me know if you face any query regarding this OR require more help.

8
  • Thank you for attention! Where it is necessary to insert the code? I put to the end of the code and the site stopped working (white screen). An error at the end of the code, this latter function. I see this as a Problem related to the function of creating events (my theme) I do not use it, it is possible to disable this feature completely? Commented Aug 1, 2015 at 4:35
  • Place my four line code before /* Line 602 bellow */ comment statement. Code only represent that what $request pull, based on that we can place new(solution) code. Do not place it any where. Commented Aug 1, 2015 at 5:19
  • No change! Error is continued! Here's how I put code in two versions. The only thing that I noticed this change in this post gravitation.pro/jessie-andrews Commented Aug 1, 2015 at 19:39
  • if ( is_single() && $wp_query->post_count == 0 ) { $events_cat = hoon_option( 'events_category' ); $request = $wpdb->get_results( $wp_query->request ); print('<pre>'); print_r($request); print('</pre>'); exit; Commented Aug 1, 2015 at 19:41
  • if ( is_single() && $wp_query->post_count == 0 ) { $events_cat = hoon_option( 'events_category' ); $request = $wpdb->get_results( $wp_query->request ); $request = $wpdb->get_results( $wp_query->request ); print('<pre>'); print_r($request); print('</pre>'); exit; Commented Aug 1, 2015 at 19:42

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.