3

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?

9

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;
| improve this answer | |
0

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'";
    }
| improve this answer | |
  • There's a typo in the first SQL command - missing a single quote at the end of "blue" - submitted an edit. – Rik Lewis Apr 4 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.