Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've added some code to my templates which automatically adds custom fields to cache some data that each post pulls in. Is there a way that I can track how many posts have had this custom field set?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You could run a custom query to count the amount of posts with a given key using $wpdb or more specifically using get_var(). If i assume you want to count published posts with a given key, then something like the folowing should do it..(though this is just an example).

function get_meta_count( $key = '', $type = 'post', $status = 'publish' ) {
    global $wpdb;
    // Example code only
    // Good idea to add your own arg checking here
    if( empty( $key ) )
        return;
    $r = $wpdb->get_var( $wpdb->prepare( "
        SELECT COUNT(*) as count FROM {$wpdb->postmeta} pm
        LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id 
        WHERE pm.meta_key = '%s' 
        AND p.post_status = '%s' 
        AND p.post_type = '%s'
    ", $key, $status, $type ) );
    return $r;
}

Echo or store the value returned as needed. If you're intending on passing in any data that's been requested or sent by a user, then you'll need to add some further data checking inside the function.

NOTE: If you're doing this counting on an admin page, then there may be an internal function you can use instead(i can't remember specifically which right now, but i'll look it up if you need something admin side).

Hope that helps in any case.

Addition: I was curious after Viper posted to see how much difference there would be between the two queries, so i did each call 5 times, and recorded the time..

$wdbb->get_var 

Script Execution Time: 0.004 seconds
Script Execution Time: 0.004 seconds
Script Execution Time: 0.004 seconds
Script Execution Time: 0.004 seconds
Script Execution Time: 0.004 seconds

// Yes i realise the results are all the same above, that's how they came out

get_posts() 

Script Execution Time: 0.035 seconds 
Script Execution Time: 0.04 seconds 
Script Execution Time: 0.03 seconds
Script Execution Time: 0.027 seconds
Script Execution Time: 0.04 seconds 

I would note, when get_posts was given a meta_key it couldn't find, then it gave results back much closer to the $wpdb speed, but still fell short, leaving the $wpdb method at least 2 times faster each time.

Think my results are flawed? Please do perform your own tests, and should you want a copy of the code i used, you can find it in barebones form here.

Viper does have a valid point(future proofing is always a good thing), i just wanted to show there are some measurable differences between the two methods(and if anyone sees a problem with my approach, please feel free to let me know).

share|improve this answer
    
Perfect, thanks! –  quis Feb 21 '11 at 15:59
    
You're welcome.. :) –  t31os Feb 21 '11 at 23:18
add comment

I'd recommend using get_posts(). It'll use up more memory than directly querying the database as it'll be returning the full post objects, but it has the advantage of being essentially future proof as you're using the API.

$meta_posts = get_posts( 'meta_key=foobar' );
$meta_post_count = count( $meta_posts );
unset( $meta_posts);

See the query_posts() docs for a full list of arguments that you can use and a ton of great examples.

share|improve this answer
    
+1 - A good tip to future proof code.. :) –  t31os Feb 22 '11 at 0:12
    
This would be nice if it worked, but from the documentation for get_posts(): $meta_key and $meta_value(string) (optional): Only show posts that contain a meta (custom) field with this key and value. Both parameters must be defined, or neither will work. –  quis Feb 22 '11 at 8:38
    
@quis: That is no longer the case. See codex.wordpress.org/Function_Reference/… –  Viper007Bond Mar 7 '11 at 23:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.