AnswerBun.com

Woocommerce Keep custom Sorting and sold out products in the back - hide theme element

WordPress Development Asked by Diederick L. on October 24, 2020

I’m trying to get my products sorted.
I’ve found a code that has multiple been discussed on these forums and on other sites.
It works perfect for the sorting.
But it targets one of the elements that is used in my theme Generatepress.
And therefore it’s filtered out.
Since I’m not familiar with these queries I guess I’ll try it here.

The code that I use is the following:

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;
}

If I monitor the query I get this:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_relationships AS tt1
ON (wp_posts.ID = tt1.object_id)
LEFT JOIN wp_icl_translations wpml_translations
ON wp_posts.ID = wpml_translations.element_id
AND wpml_translations.element_type = CONCAT('post_', wp_posts.post_type)
INNER JOIN wp_postmeta istockstatus
ON (wp_posts.ID = istockstatus.post_id)
WHERE 1=1
AND istockstatus.meta_key = '_stock_status'
AND istockstatus.meta_value <> ''
OR wp_posts.post_type = 'gp_elements'
AND ( wp_term_relationships.term_taxonomy_id IN (1816)
AND wp_posts.ID NOT IN (
SELECT object_id
FROM wp_term_relationships
WHERE term_taxonomy_id IN (10) )
AND tt1.term_taxonomy_id IN (1816) )
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')
AND ( ( ( wpml_translations.language_code = 'nl'
OR ( wpml_translations.language_code = 'nl'
AND wp_posts.post_type IN ( 'gp_elements' )
AND ( ( (
SELECT COUNT(element_id)
FROM wp_icl_translations
WHERE trid = wpml_translations.trid
AND language_code = 'nl' ) = 0 )
OR ( (
SELECT COUNT(element_id)
FROM wp_icl_translations t2 JOIN wp_posts p
ON p.id = t2.element_id
WHERE t2.trid = wpml_translations.trid
AND t2.language_code = 'nl'
AND ( p.post_status = 'publish'
OR p.post_type='attachment'
AND p.post_status = 'inherit' ) ) = 0 ) ) ) )
AND wp_posts.post_type IN ('post','page','attachment','wp_block','product','product_variation','gp_elements' ) )
OR wp_posts.post_type NOT IN ('post','page','attachment','wp_block','product','product_variation','gp_elements' ) )
GROUP BY wp_posts.ID
ORDER BY istockstatus.meta_value ASC, wp_posts.menu_order ASC, wp_posts.post_title ASC
LIMIT 0, 20

So at the last lines you can see the query targets wp_posts.post_type.
And in that statement it gives gp_elements.
These need to be included in my where clause so they aren’t filtered out.

Any suggestions?

Add your own answers!

Related Questions

How can I noindex my archive pages?

1  Asked on November 26, 2021 by summer-pratt

     

admin-ajax error 400

0  Asked on November 26, 2021 by daniel-nebert

   

Bulk update custom post types

1  Asked on November 26, 2021 by samm-foolt

     

Upload avatar for post comment

1  Asked on November 26, 2021 by divyang

     

Non-blocking file_put_contents in function.php

2  Asked on November 26, 2021 by mustafa

   

Image problem – Multisite image upload broken

1  Asked on November 23, 2021 by prakhar-monga

   

Where is the htaccess in wordpress.com hosting?

1  Asked on November 23, 2021 by user2007920

   

How do you approach block structured content?

1  Asked on November 23, 2021 by kevin-all

     

Google Fonts not loading correctly

1  Asked on November 23, 2021 by octavialo

   

wp_redirect() function is not working

8  Asked on November 21, 2021 by sans780730

   

How to search only by post title and category?

1  Asked on November 21, 2021 by carl-jue-nier

     

Custom form validation

1  Asked on November 21, 2021 by alexander-holsgrove

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP