Support » Developing with WordPress » Count posts and metavalues

  • Hi,
    I would like to achieve the following:
    1: Count number of posts within a post_type and taxonomy-term within a certain timeframe. (DONE)
    2: Count a metavalue within the previous result and combine the results as one final return (NOT DONE)

    Code for no.1:

    function antall_ta5_mnt($avdeling = ALL, $korr = '0', $mnt = 'm') {
    	$posts = array(
    	'post_type'			=> 'ta5',
    	'numberposts'		=> -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field'    => 'slug',
                'terms'    => $avdeling
           ),
        ),
    	'date_query' => array(
            array(
                'year' => date( 'Y' ),
                'month' => date( $mnd ) -$korr,
            ),
        ),
    	);
    	$actual_mnt = date('m');
    	$posts = get_posts($posts);
    	$counted = count($posts);
    	if(($counted < 1) && ($mnt > $actual_mnt)) {
    	return NULL;
    	} else {
    	return $counted;
    	}
    }

    Partial code for no.2:

    	$query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'meta_deltakere'";
    
    	$words = $wpdb->get_results($query);
    	if ($words)
    	{
    		foreach ($words as $word)
    		{
    			$meta = strip_tags($word->meta_value);
    			$meta = explode(' ', $meta);
    			$count = count($meta);
    			$totalcount = $count;
    		}
    	}
    	else
    	{
    		$totalcount = 0;
    	}

    As you can see, the second code does not talk with the first one…

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You need a second get_posts() query matching the first with the addition of a “meta_query” argument specifying that a meta value exists under that key for all posts found. If you want a count of posts with the value, use count() again. If you need a sum total of all meta values, it’s probably easiest to loop through the results, getting each meta value in turn and keeping a running tally.

    Or if you need a sum of values, use the “fields” argument so you only get post IDs from get_posts(), then make a custom SQL query to SUM() all matched meta values where the post id is in the list of IDs and the meta key = ‘meta_deltakere’. This would be more efficient, but writing custom SQL queries isn’t in everyone’s repertoire.

    Thread Starter MrConn

    (@mrconn)

    Thank you for your response!
    I’ve been fiddeling with a second get_posts(), but I only manage to get the count from the post with the highest count…?

    
    function antall_deltakere_mnd($avdeling = ALL, $korr = '0', $mnd = 'm')
    {
        $posts = array(
            'post_type' => 'ta5',
            'numberposts' => - 1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'ta5_avdeling',
                    'field' => 'slug',
                    'terms' => $avdeling
                ) ,
            ) ,
            'date_query' => array(
                array(
                    'year' => date('Y') ,
                    'month' => date($mnd) - $korr,
                ) ,
            ) ,
            'meta_query' => array(
                array(
                    'key' => 'meta_deltakere',
                ) ,
            ) ,
        );
        $words = get_posts($posts);
        if ($words)
        {
            foreach ($words as $word)
            {
                $meta = strip_tags($word->meta_deltakere);
                //$meta = explode(' ', $meta);
                $meta = explode(",", str_replace(" ", "", $meta));
                $count = count($meta);
                $totalcount = $count;
    
            }
            return $count;
        }
        else
        {
            return $totalcount = 0;
        }
    }
    

    Can anyone please point me in the right direction? 🙂

    Moderator bcworkz

    (@bcworkz)

    Oh, good effort! The problem is you’re overwriting the running total instead of adding to it. You want $totalcount += $count;. You are also failing to return the right variable. You want return $totalcount;

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.