/***Function to count posts by meta key & value Use: count_seed_sale('POST_TYPE', 'META_KEY', 'META_VALUE')******/ function count_seed_sale($rpost, $metakey, $mvalue){ $args = array( 'post_type' => $rpost, 'meta_key' => $metakey, 'meta_value' => $mvalue, 'posts_per_page' => -1, ); $query = new WP_Query($args); $count = 0; //$post_ids = []; if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); //$post_ids[] = get_the_ID(); // do something with the post ID //echo get_the_ID(); $count ++; } wp_reset_postdata(); } return $count; } /***Function to count posts by meta key & value ******/
OR
function count_seed_sale_total($rpost, $metakey, $mvalue){ $args = array( 'post_type' => $rpost, 'meta_key' => $metakey, 'meta_value' => $mvalue, 'numberposts' => -1, 'post_status' => 'publish', ); $query = get_posts($args); $count = 0; //$post_ids = []; $total =0 ; foreach ( $query as $post ) { //$post_ids[] = $post->ID; // do something with the post ID //echo get_the_ID(); $count ++; } return $total; }
Jagdish Sarma Changed status to publish March 25, 2023