• Gazow

    (@gazow)


    I’m trying to get a list of tags, that also match a particular category, but the problem I have is that it also returns tags with 0 stock inventory ( using someting like $tag->count isnt working because it counts products that have 0 stock)

    not sure how I can check stock in each tag to omit from the list-

    heres the function I have for getting the full tag list- (cat is the actual category passed and ptag is just the taxonomy name ‘product_tag’

    function get_category_tags($pcat, $ptag) {
    
    $pcat =get_term_by( 'slug', $pcat, 'product_cat' );
    $pcat = $pcat->term_id;
    
    global $wpdb; 
    
    $args= "SELECT DISTINCT
    terms2.term_id as tag_ID,
    terms2.name as tag_name,
    terms2.slug as tag_slug,
    t2.count as posts_with_tag
    FROM
    $wpdb->posts as p1
    LEFT JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
    LEFT JOIN $wpdb->term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = terms1.term_id, 
    
    $wpdb->posts as p2
    LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID = r2.object_ID
    LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms2 ON t2.term_id = terms2.term_id 
    
    WHERE (
    t1.taxonomy = 'product_cat' AND
    p1.post_status = 'publish' AND
    terms1.term_id = '".$pcat."' AND
    t2.taxonomy =  '".$ptag."' AND
    p2.post_status = 'publish' AND
    p1.ID = p2.ID
    )
    ORDER by tag_name";
    
    $tags = $wpdb->get_results($args);
    
    $count = 0;
    foreach ($tags as $tag) {
    $tags[$count]->tag_link = get_tag_link($tag->tag_id);
    $count++;
    }
    return $tags;
    }

    i think i need to ad something along the lines of the following but i cant seem to make it work

    JOIN $wpdb->postmeta as pm ON p1.ID = pm.post_id
        AND pm.meta_key = '_stock_status'
        AND pm.meta_value = 'instock'

    https://www.ads-software.com/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    How many products? If its not too large, it might be easier to follow if you grab a list of in stock post_ids first, then pass those ids to the big query.

Viewing 1 replies (of 1 total)
  • The topic ‘Does anyone know how to get from $wpdb posts that have instock meta?’ is closed to new replies.