Support

Account

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

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