• Resolved seyedmohsen

    (@seyedmohsen)


    Hello, I use the following code that I found on the internet to move non-existent products to the bottom of the page and display the products in order of publication date, and it works correctly. But after installing the web story plugin and trying to create a story in product categories, I realized that this code causes the story not to be displayed. It means that the web story does not work in the pages where my product list is displayed.
    Please help me in this problem

    /** Show Out of stock products at the end in Woocommerce */
    add_filter('posts_clauses', 'order_by_stock_status');
    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() || is_product_taxonomy())) {
            $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;
    }
Viewing 1 replies (of 1 total)
  • Plugin Author Pascal Birchler

    (@swissspidy)

    Hi there

    If you use the post_clauses filter without any conditionals, this will affect all queries on the page, so be careful.

    As per the documentation, the current WP_Query instance is passed as the second argument to the filter. You should use that to ensure you only modify the one query you need. Just adding a check for $query->is_main_query() should do the trick.

    add_filter('posts_clauses', 'order_by_stock_status', 10, 2);
    function order_by_stock_status($posts_clauses, $query) {
    if ( ! $query->is_main_query() ) { return false; }
    // ... rest of your logic
    }

    For further help with custom post_clauses filtering I recommend asking in the general support forums or the WordPress Development Stack Exchange, as this is out of scope for our support offering.

    Good luck!

Viewing 1 replies (of 1 total)
  • The topic ‘Interference of a code with a web story’ is closed to new replies.