Count post that have specific meta value

All we need is an easy explanation of the problem, so here it is.

How do I count only the post that have a specific custom meta field value?

$productCount = wp_count_posts('product');
echo $productCount->publish;

That gives me all the total of all the post.

How do I find out how many of those post have say a custom meta value of cat?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

There is no default/native function in wordpress to count posts from a specific meta value. wp_count_posts only counts the amount of posts according to post type

It might be useful here to use WP_Query to create a custom query to get all posts with that specific meta value and then use $wp_query->found_posts to get the post count.

This should do it

 $query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue' ) );

   echo $query->found_posts;

Method 2

Probably the best way to do this is by:

global $wpdb;
$count = $wpdb->get_row("SELECT COUNT(*) AS THE_COUNT FROM $wpdb->postmeta WHERE (meta_key = 'color' AND meta_value = 'blue')");

return $count->THE_COUNT;

Here’s a collection of functions to make it easier to use:

    /**
     * Returns the number of posts a particular metakey is used for.
     * Optionally qualified by a specific value or values in an array of the meta key.
     *
     * @param string            $key
     * @param null|string|array $value
     *
     * @return mixed
     */
    function get_meta_count( $key, $value = null) {
        global $wpdb;

        $where = get_meta_where($key, $value);
        $count = $wpdb->get_row("SELECT COUNT(*) AS THE_COUNT FROM $wpdb->postmeta WHERE $where");
        return $count->THE_COUNT;
    }

    /**
     * Returns the postmeta records for a particular metakey.
     * Optionally qualified by a specific value or values in an array of the meta key.
     *
     * @param string            $key
     * @param null|string|array $value
     *
     * @return mixed
     */
    function get_meta ( $key, $value = null) {
        global $wpdb;
        $where = get_meta_where($key, $value);
        return $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE $where");
    }

    /**
     * Returns the where string for a metakey query
     *
     * @param string            $key
     * @param null|string|array $value
     *
     * @return string
     */
    function get_meta_where( $key, $value) {
        $where = "meta_key = '$key'";

        if (null !== $value) {
            if (\is_array($value)) {
                \array_walk($value,'add_quote');
                $in = \implode(',', $value);  // Seperate values with a comma
                $where .= " AND meta_value IN ($in)";
            } else {
                $where .= " AND meta_value = '$value'";
            }
        }

        return $where;
    }

    /**
     * Puts quotes around each value in an array when used as a callback function
     *
     * @param $value
     * @param $key
     */
    function add_quote( &$value, $key)
    {
        $value = "'$value'";
    }

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply