0

i have shop on wordpress/woocommerce. When I added function to have sold out products at the end of list I'm getting warnings:

Warning: Attempt to read property "ID" on null in .../ftp/wp/wp-includes/class-wp-query.php on line 4044

Warning: Attempt to read property "post_title" on null in .../ftp/wp/wp-includes/class-wp-query.php on line 4046

Warning: Attempt to read property "post_name" on null in .../ftp/wp/wp-includes/class-wp-query.php on line 4048

Warning: Attempt to read property "post_type" on null in .../ftp/wp/wp-includes/class-wp-query.php on line 4196

Warning: Undefined array key 0 in .../ftp/wp/wp-includes/capabilities.php on line 76

lines from 4031 to 4064:

public function is_page( $page = '' ) {
        if ( ! $this->is_page ) {
            return false;
        }

        if ( empty( $page ) ) {
            return true;
        }

        $page_obj = $this->get_queried_object();

        $page = array_map( 'strval', (array) $page );

        if ( in_array( (string) $page_obj->ID, $page, true ) ) {
            return true;
        } elseif ( in_array( $page_obj->post_title, $page, true ) ) {
            return true;
        } elseif ( in_array( $page_obj->post_name, $page, true ) ) {
            return true;
        } else {
            foreach ( $page as $pagepath ) {
                if ( ! strpos( $pagepath, '/' ) ) {
                    continue;
                }
                $pagepath_obj = get_page_by_path( $pagepath );

                if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
                    return true;
                }
            }
        }

        return false;
    }

code for sorting products:

/**
* Sorting out of stock WooCommerce products - Order product collections by stock status, in-stock products first.
*/
class iWC_Orderby_Stock_Status
{
public function __construct()
{
// Check if WooCommerce is active
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
add_filter('posts_clauses', array($this, 'order_by_stock_status'), 2000);
}
}
public function order_by_stock_status($posts_clauses)
{
global $wpdb;
// only change query on WooCommerce loops
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag())) {
$posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
$posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
$posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
}
return $posts_clauses;
}
}
new iWC_Orderby_Stock_Status;
/**
* END - Order product collections by stock status, instock products first.
*/

when i removed those code i don't get warnings.

Any ideas how to remove those warnings?

my shop: customoweplugi.pl

user16572932
  • 1
  • 1
  • 1
  • 2
  • 4
    Make sure `$page_obj` contains what is expected and is not null if it is null do not attempt to access any properties – empiric Sep 08 '21 at 11:22
  • 1
    Does this answer your question? [PHP check whether property exists in object or class](https://stackoverflow.com/questions/14414379/php-check-whether-property-exists-in-object-or-class) – empiric Sep 08 '21 at 11:26
  • I'm not into php, so i don't have idea what can be wrong and how to fix it – user16572932 Sep 08 '21 at 11:32
  • 2
    The code you posted comes from [an existing WordPress core file](https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-query.php#L4054-L4102). **It is not recommended to change WordPress core files to fix issues**. _"When I added function to have sold out products at the end of list I'm getting warnings"_ you should add that code to your question, versus the code from WordPress – 7uc1f3r Sep 08 '21 at 12:21

1 Answers1

1

If you do not know what impact the changes you have made may have on the code and you don't understand it at least to the point that you are confident that those changes won't mess anything, then don't make those changes in the first place!

I do not recommend it, but if you just don't want to see this warnings then you can use an "Error Control Operator". Just add "@" before your expression like:

@$page_obj->ID

IT WONT SOLVE THE ERRORS, but will only prevent them from displaying!

Read more about Error Control Operators here: https://www.php.net/manual/en/language.operators.errorcontrol.php

whocares4400
  • 106
  • 7