Support

Account

Home Forums General Issues Count posts by checked value (checkbox)

Solving

Count posts by checked value (checkbox)

  • Hi,

    I’m trying to count the posts by value and metakey. So far it’s working for radio buttons but not for a checkbox. This is what I got now:

    function get_post_count_by_meta( $meta_key = false, $meta_value = false, $post_type = 'post' ) {
    	
    		
    	$args = array(
    			'post_type' => $post_type,
    			'numberposts'	=> -1,
    			'post_status'	=> 'publish',
    		);
    		
    		if ( $meta_key && $meta_value ) {
    			$args['meta_query'][] = array('key' => $meta_key, 'value' => $meta_value);	
    		}
    		elseif ( $meta_value ) {
    			$args['meta_query'][] = array('value'=>$meta_value);	
    		}
    		elseif ( $meta_key ) {
    			$args['meta_query'][] = array('key' => $meta_key);	
    		}
    		
    		$posts = get_posts($args);
    		
    		$count = count($posts);
    		
    	
    	return $count;
     
    }
     $post_count = get_post_count_by_meta('aangeboden_door', 'ergotherapie', 'any');
    echo $post_count;

    How to make this work for a checkbox where multiple values are possible? Probably I need to make somewhere a loop from it?

  • So you want to count posts that have a field named ‘aangeboden_door’ with several different values? I think you’ll have to modify the function so as to add meta_value as an array (of values) and setting ‘compare’ => ‘IN’. Try something like this:

     
    // ...
    if ( $meta_key && $meta_value ) {
    	if ( is_array($meta_value) ) {
    	        $args['meta_query'][] = array('key' => $meta_key, 'value' => $meta_value, 'compare' => 'IN');
            }
            else {
    	        $args['meta_query'][] = array('key' => $meta_key, 'value' => $meta_value);
            }	
    }
    // ...
    
  • You also might have to add ‘type’ => ‘numeric’ depending on the values. See: http://codex.wordpress.org/wp_query#Custom_Field_Parameters

  • Hi Wells,

    I tried several things, also the array but or I get 0, or I get the number of all posts in the post-type.

    This gives me 0:

    function get_post_count_by_meta( $meta_key, $meta_value, $post_type = 'post' ) {
    
    	$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' => 'IN');
    	    }
    		
    		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('niveau_jaren', 'Jaar 2', 'any');
    echo $post_count; 

    But I have an array that shows me all the posts that are checked with ‘Jaar 2’ which works:

     $args = array(
    	'numberposts' => -1,
    	'post_type' => 'any',
    	'meta_query' => array(
    		
    		array(
    			'key' => 'niveau_jaren',
    			'value' => 'Jaar 2',
    			'compare' => 'LIKE'			
    		),
    		
    	
    )
    );

    Now how would I ‘translate’ this array to the code above? Thanks!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.