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

WordPress Importer “Failed to import Media”

0  Asked on January 19, 2021 by orbita

 

Override theme file from plugin

0  Asked on January 18, 2021 by toma-tomov

 

Why can I not view image attachment pages?

3  Asked on January 17, 2021 by h-hall

     

Image color changes in wordpress

0  Asked on January 16, 2021 by giulia

     

Inserting additional   into menu item text

2  Asked on January 16, 2021 by marsandback

 

wp_mail() not sending emails with ajax

2  Asked on January 16, 2021 by mohd-hasan

   

How to ignore fields if empty?

1  Asked on January 16, 2021 by israel-santiago

   

Change WordPress post-state in Admin Area

2  Asked on January 16, 2021 by gino

   

Select posts with any post_type from database?

1  Asked on January 15, 2021 by lisandro-vaccaro

 

Why does WP_Post not contain its permalink?

3  Asked on January 14, 2021 by dodov

   

How to check post type when using sanitize_title hook?

1  Asked on January 14, 2021 by michael-rogers

 

WordPress retrieve media except cropped

0  Asked on January 12, 2021 by adviner

 

You have an error in your SQL syntax – Help with query

2  Asked on January 11, 2021 by ogmios

   

Is it possible to display tag cloud widget by category?

1  Asked on January 11, 2021 by taller-de-wordpress

   

Ask a Question

Get help from others!

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