最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Count post that have specific meta value

programmeradmin3浏览0评论

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 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?

Share Improve this question edited Oct 23, 2014 at 18:21 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Jun 18, 2014 at 3:11 user12920user12920 2071 gold badge4 silver badges8 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 11

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;

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'";
    }
发布评论

评论列表(0)

  1. 暂无评论