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 added a content rating system to my platform where the authors can select which audience their post is appropriate for. Currently, these options are available:

Unrated

G

PG

R

The code that I use to display the rating options on the post edit page is:

// Article Content Rating
add_action( 'add_meta_boxes', 'rating_select_box' );
function rating_select_box() {
    add_meta_box(
        'rating_select_box', // id, used as the html id att
        __( 'Content Rating (optional)' ), // meta box title
        'rating_select_cb', // callback function, spits out the content
        'post', // post type or page. This adds to posts only
        'side', // context, where on the screen
        'low' // priority, where should this go in the context
    );

}

function rating_select_cb( $post ) {
    global $wpdb;
    $value = get_post_meta($post->ID, 'rating', true);
    echo '<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Article Content Rating: </label>';

    $ratings = array(
        1 => ' G ',
        2 => ' PG ',
        3 => ' R ',
    );

    echo '<select name="rating">';
    echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> Unrated </option>';

    // output each rating as an option
    foreach ($ratings as $id => $text) {
        echo '<option value="' . $id . '"' . (($value == $id) ? ' selected="selected"' : '') . '">' . $text. '</option>';
    }
    echo '</select>';

    echo '</span></div>';
}

add_action( 'save_post', 'save_metadata');

function save_metadata($postid)
{   
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( 'edit_page', $postid ) ) return false;
    if( empty($postid) ) return false;


    if ( is_null($_REQUEST["rating"]) ) {
        delete_post_meta($postid, 'rating');
    } else {
        update_post_meta($postid, 'rating', $_REQUEST['rating']);
    }

}
// END Article Content Rating

Now, the problem is, what code do I add to single.php to display their choice? So for instance, if the author selected PG, then I want to echo 'Content Rating: PG'; or if it was on default (unrated), I want to echo 'Content Rating: Unrated';. How is this possible? Ideally, a solution that is light on the server as my platform is heavily trafficked.

share|improve this question
add comment

3 Answers

You are already doing what you need to do, but in another context.

$value = get_post_meta($post->ID, 'rating', true);

Take that value and format it for display.

Edit: I installed your whole block of code. You are storing you key, not your ratings value. You need to access that $ratings array again to pull the value.

$ratings = array(
    1 => ' G ',
    2 => ' PG ',
    3 => ' R ',
);

if (!empty($ratings[$value])) {
  echo '<p>Content Rating : '.$ratings[$value].'</p>';
} else {
  echo '<p>Content Rating : Unrated</p>';
}

However, even before I spotted the problem I was seeing content echo. "Content Rating: 1" instead of "Content Rating: G". I don't know why you claim to see nothing. If you still see nothing, something is wrong but not with this code.

I made no changes to your original block of code.

share|improve this answer
    
This seems to echo everything as Content Rating : Unrated no matter the choice. Do you have a sandbox to test it yourself? –  Christine Cooper Jan 3 '13 at 14:51
    
That 'get_post_meta` line returns a string for the appropriate meta_key. It should work. You need to debug. Try var_dump($post->ID,$value); right after the get_post_meta line. –  s_ha_dum Jan 3 '13 at 14:59
    
I added var_dump($post->ID,$value); after the $value = get_post_meta($post->ID, 'rating', true); line and refreshed and tested everything again, what should I be looking for as the same results were displayed? If I sound unexperienced in this matter, it is because I am. –  Christine Cooper Jan 3 '13 at 15:05
    
You want a number followed by a string-- one of your ratings values. Just post the output. –  s_ha_dum Jan 3 '13 at 15:07
    
All that is output is Content Rating : Unrated. –  Christine Cooper Jan 3 '13 at 15:10
show 5 more comments

Inspecting the meta data (var_dump or FirePHP), we can see that the meta is stored as an associative array with one pair of $key=>$value.

The key is always 0 and the value, the number of the selected rating.
E.g.: array('0'=>'1') for G.

So, this little function returns the correct rating:

function wpse_78113_get_rating( $pid )
{
    $ratings = array(
        1 => ' G ',
        2 => ' PG ',
        3 => ' R ',
    );
    $meta = get_post_meta( $pid, 'rating' );
        // var_dump($meta);

    if( empty($meta) || empty($meta[0]) )
        return 'Content rating: Unrated';

    return 'Content rating: ' . $ratings[$meta[0]];
}

And use it like: echo wpse_78113_get_rating( $post->ID );

share|improve this answer
add comment
if ( is_null($_REQUEST["rating"]) ) {
    delete_post_meta($postid, 'rating');
} else {
    update_post_meta($postid, 'rating', $_REQUEST['rating']);
}

Does this not need to be $post->ID in the save_meta function?

share|improve this answer
2  
Is $post->ID haunting you? ;) Look closer, that's the parameter received by save_metadata($postid)... –  brasofilo Jan 4 '13 at 15:33
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.