Home › Forums › General Issues › Count of posts with the same field choice
Hi Elliot,
I’m trying to get the count of posts that have the same field choice selected. Can you give me hint how to do this?
Try this function: https://gist.github.com/wells5609/5944561
It counts posts of a given post type with either a meta_key, meta_value, or key/value pair. Results are cached for 3 hours (you can change this depending on how often the post count will change). Use in a template like:
$post_count = get_post_count_by_meta('some_key', 'some_value', 'some-post-type');
echo $post_count;
@wells5609 If you could help me a bit further, that would be great.
I’m trying to connect my field group to a ajax filter plugin. It should be able to just put the field key in the shortcode and it should work but it does not so I’m trying to edit the plugin a bit. I’ve been able to show the correct field list in the plugin, but it does not show the counts or use the filter function..
This is one part of the code (uag-wall-functions.php):
if (strstr($key,'metakey')) {
$matches = array();
if (preg_match_all('/{([^}]*)}/', $value, $matches)) {
$i = 0;
foreach($matches[1] as $array) {
$i++;
$all_arg = explode(',', $array);
foreach($all_arg as $arg) {
$str_arr = explode(':', $arg);
$metakey[$i][$str_arr[0]] = $str_arr[1];
}
/* ARGS */
$key = (isset($metakey[$i]['key'])) ? $metakey[$i]['key'] : null;
$field_key = (isset($metakey[$i]['field_key'])) ? $metakey[$i]['field_key'] : null;
$title = (isset($metakey[$i]['title'])) ? $metakey[$i]['title'] : __('Untitled','uag');
$reset_text = (isset($metakey[$i]['reset_text'])) ? $metakey[$i]['reset_text'] : __('Reset','uag');
$metavalues = get_field_object($field_key);
if( $metavalues )
{
$display .= '<div class="uag-filter" data-key="'.$field_key.'">
<div class="uag-filter-head"><span class="uag-toggle"></span>'.$title.'<div class="uag-helpers"><span class="uag-loading uag-hide"></span><a href="#" class="uag-reset">'.$reset_text.'</a></div></div>
<div class="uag-mainbody">
<div class="uag-filter-body">';
foreach($metavalues['choices'] as $metavalue) {
$display .= '<a href="#" class="uag-filter-option" data-metakey="'.$field_key.'" data-metavalue="'.$metavalue.'">'.$metavalue;
if ($args['count'] == 'show') {
$display .= '<span class="uag-count">'.uag_meta_count($field_key, $metavalue, $args).'</span>';
}
$display .= '</a>';
}
$display .= '</div>
</div>
</div>';
}
and the functions.php file:
/* Collect meta values */
function uag_meta_values($metakey) {
global $wpdb;
$prefix = $wpdb->prefix.'postmeta';
$posts = $wpdb->prefix.'posts';
$values = $wpdb->get_col("SELECT meta_value AS $metakey FROM $posts
JOIN $prefix ON ($prefix.post_id = $posts.ID)
WHERE post_status = 'publish'
AND meta_key = '$metakey'");
$values = array_unique($values);
return $values;
}
/* Count */
function uag_count_meta_values($metakey) {
global $wpdb;
$prefix = $wpdb->prefix.'postmeta';
$posts = $wpdb->prefix.'posts';
$values = $wpdb->get_col("SELECT meta_value AS $metakey FROM $posts
JOIN $prefix ON ($prefix.post_id = $posts.ID)
WHERE post_status = 'publish'
AND meta_key = '$metakey'");
$values = array_unique($values);
return count($values);
}
/* meta count by value */
function uag_meta_count($metakey, $metavalue, $args = null) {
$loop = array(
'post_type' => 'any',
'posts_per_page' => -1,
'meta_key' => $metakey,
'meta_value' => $metavalue,
);
Sorry, it’s a lot of code. But actually I’m not sure where to start in here. If you would point me in the good direction that would be enough. Thanks
Hi @wells5609. Your git link is out of reach. Can you publish it again? I have the get_post_count_by_meta() function, but this return warning (Warning: trim() expects parameter 1 to be string, array given in) that make my site very slow. I just need to know if your function is the same function that I found on internet.
@LeffDesign, your function is working without issues?
Thanks for your attention.
@cardosus – I’m very late to the game here, but as this still comes up in Google – here’s a link to the full function in a StackOverflow answer, and also the function below (in case the SO answer also disappears):
function get_post_count_by_meta( $meta_key, $meta_value, $post_type) {
$args = array(
'post_type' => $post_type,
'numberposts' => -1,
'post_status' => 'publish',
);
if ( $meta_key && $meta_value ) {
if ( is_array($meta_value) ) {
$args['meta_query'][] = array(
'key' => $meta_key,
'value' => $meta_value,
'compare' => 'LIKE'
);
} else {
$args['meta_query'][] = array('key' => $meta_key, 'value' => $meta_value);
}
}
$posts = get_posts($args);
$count = count($posts);
return $count;
}
$post_count = get_post_count_by_meta('test_field', 'Value 1', 'any');
echo $post_count;
The topic ‘Count of posts with the same field choice’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.