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 have a code in my functions.php that gives me a count of how many favorites a post has got.

function wpfp_get_current_count() {
global $wpdb;
$current_post = get_the_ID();
$query = "SELECT post_id, meta_value, post_status FROM $wpdb->postmeta";
$query .= " LEFT JOIN $wpdb->posts ON post_id=$wpdb->posts.ID";
$query .= " WHERE post_status='publish' AND meta_key='wpfp_favorites' AND post_id = '".$current_post."'";
$results = $wpdb->get_results($query);
if ($results) {
    foreach ($results as $o):
        echo $o->meta_value;
    endforeach;
}else {echo( '0' );}}

And I use the following code to achieve the count and it works just fine.

<?php echo 'TOTAL FAVORITES: ' .wpfp_get_current_count(); ?>

Now, I would like to add 300 favorites by default. So that if my actual favorites are 33 then the total favorites becomes 333. I guess i need a lesson on some basic php addition. If possible please suggest me a modified code. I tried the code below, but the addition does not happen. To be honest I asked this question earlier here on wordpress.org frorum but could not get a reply. Please help.

share|improve this question
    
Do you want to add 300 favorites by default when displaying numbers or do you want to save it in database? –  Vinod Dalvi Apr 16 '13 at 10:25
    
I simply want any of my post to have a minimum of 300 favorite count to begin with. So basically if it means to save it in database then yes. –  gurung Apr 16 '13 at 10:30

2 Answers 2

up vote 0 down vote accepted

You can simply do it by adding 300 when displaying count as shown in following code:

<?php echo 'TOTAL POST VIEWS: ' .(intval(wpfp_get_current_count()) + 300); ?>

and instead of echoing that values in function return it as shown in following code:

function wpfp_get_current_count() {
global $wpdb;
$current_post = get_the_ID();
$query = "SELECT post_id, meta_value, post_status FROM $wpdb->postmeta";
$query .= " LEFT JOIN $wpdb->posts ON post_id=$wpdb->posts.ID";
$query .= " WHERE post_status='publish' AND meta_key='wpfp_favorites' AND post_id = '".$current_post."'";
$results = $wpdb->get_results($query);
if ($results) {
foreach ($results as $o):
    return $o->meta_value;
endforeach;
}else { return 0;}}

For more information on intval() function refer this page.

share|improve this answer
    
Yep, that did the job. You are awesome. Thank you and thanks to this great community. –  gurung Apr 16 '13 at 10:42

The WPDB Class has a function specifically designed to return one value from a query, $wpdb->get_var() returns a single value or null if there is not a result.

I'm going to provide an alternative code sample for the function, so you can see it approached another way.

function wpfp_get_count( $id = false ) {

    global $wpdb;

    if( !$id )
        $id = get_the_ID();

    $favorite_count = $wpdb->get_var( $wpdb->prepare( "
        SELECT CAST(COALESCE(pm.meta_value,0) AS UNSIGNED) 
        FROM {$wpdb->postmeta} pm 
            LEFT JOIN {$wpdb->posts} p 
            ON pm.post_id = p.ID 
        WHERE p.post_status = 'publish' 
        AND pm.meta_key = 'wpfp_favorites' 
        AND p.post_id = %d 
        LIMIT 1
    ", $id ) );

    if( !$favorite_count )
        return 300;

    return $favorite_count + 300;
}

We cast the meta value to an integer right inside the query and also deal with the null value returned from the join(say if there's no record to join).

Not sure why you want to add 300 to the result, but that's what you asked for, so i just put that directly into the function(it could also be a secondary parameter in the function if you wanted to add different amounts depending on where you call it).

More on the WPDB class can be found here.

Example usage:

<?php printf( 'TOTAL FAVORITES: %d', wpfp_get_count() ); ?>

or if you want to pass an ID along..

<?php printf( 'TOTAL FAVORITES: %d', wpfp_get_count( $var_with_id ) ); ?>

Hope that helps.

share|improve this answer

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.