Support

Account

Home Forums General Issues Count of posts with the same field choice Reply To: Count of posts with the same field choice

  • @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