Custom colors for post rows based on post meta value

I’m trying to have the post background colors change depending on a meta value for the specific post type rather than using the post status. Looked everywhere and can’t find a solution. (perhaps there isn’t one?)

Specifying post colors based on post-status is simple enough

Read More
add_action('admin_footer','posts_status_color');
function posts_status_color(){
?>
<style>
.status-draft{background: #FFFF98 !important;}
.status-pending{background: #FFFF98 !important;}
.post-*id here*{background: #FFFF98 !important;}
.status-publish{/* no background keep wp alternating colors */}
</style>
<?php
}

How do I specify the colors based on a custom meta key/value from the post?

Related posts

1 comment

  1. If your theme uses wp post class

    function post_classes($classes) {
        global $post;
            $customMetaVariable = get_post_meta( $post->ID, 'customMetaName', true );
        if($customMetaVariable == 'desiredCustomMetaValue'){
            $classes[] = 'cssClassName';
            return $classes;
        }
    }
    add_filter('post_class', 'post_classes');
    

    then in your style.css you could use:

    .cssClassName{
    background-color: red;
    }
    

    hence applying that class to all of the posts that contain your desired meta value.

    if your theme doesn’t use wp post class you have to edit the theme to include

    <?php post_class(); ?>
    

    ex:

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    //post stuff hurr
    </div>
    

    all explained here.

Comments are closed.