Quantcast
Channel: Question and Answer » wpdb
Viewing all articles
Browse latest Browse all 44

Include post_status check within $wpdb query

$
0
0

I’ve got a function that returns a count from the database for publish posts that match the product ID to the post meta ID (both are the product type). I’ve realized this query returns unpublished posts as well.

I’ve tried for a couple of hours to figure out how I can include a defensive check to only include posts that have the post_status of publish

public function get_number_of_stars_for( $star_value ) {
        global $wpdb;

        return $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(wpm2.`meta_value`)
                    FROM $wpdb->postmeta as wpm2,
                    (
                        SELECT * FROM $wpdb->postmeta WHERE `meta_value` = %d AND `meta_key` = 'review_product'
                    ) as wpm1
                    WHERE wpm1.`post_id` = wpm2.`post_id` AND wpm2.`meta_key` = 'review_rating' AND wpm2.`meta_value` = %d
                ",
                $this->post->ID,             
                $star_value
            )
        );
    }

Viewing all articles
Browse latest Browse all 44

Trending Articles