I'm using post_class() function to retrieve WordPress provided classes for a post. I've successfully implemented an AJAX powered "Favorite Post" functionality to each and every post, and I'm changing the UI based on the post_class() changes. I made the following code, by which I'm checking whether a post is favorited by a User, if so, adding a class, so that I can change the UI according to it. It's working fine, but:

function project_add_favorite_class_to_post_class( $classes ) {
    global $post, $current_user;

    if( is_user_logged_in() && $post->post_type === 'cpt' ) {
        $fav_posts = get_user_meta( $current_user->ID, 'favorite_posts', true );

        if( $fav_posts && in_array( $post->ID, $fav_posts ) )
            $classes[] = 'favorite-post';

    return $classes;
add_filter( 'post_class', 'project_add_favorite_class_to_post_class' );

In single.php I added a custom code for related posts, where I excluded the current post (as it sense so). To do so, I made a function like:

function project_related_posts( $post_id = null, $numberposts = 5 ) {
if( !is_single() )

$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;

//Query according to my specifics
//Stored related Posts' IDs in an array...
//excluding $post_id
$related_posts = array( 5,6,7,8,10 );

//initiated my loop
foreach( $related_posts as $related_post_id ) :
   <div <?php post_class( 'my-class', $related_post_id ); ?>>
      //everything of the post

And in single.php I did something similar to this:

<?php the_post(); ?>
<article <?php post_class(); ?>>
   <h1><?php the_title(); ?></h1>
   <div class="entry-summary"><?php the_content(); ?></div>
<div class="related-offers">
   <?php project_related_posts( get_the_ID() ); ?>


Problem is, in single.php all the posts (even the related posts too) are taking the class 'favorite-post' if the main content is favorited.

I also tried resetting query (inside project_related_posts() function) using:

$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;


$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;

but no luck.

I'm confirming that it's not a bug of my javascripts, because my javascripts can work independently for each of the post, and even on the single page it's working independently, until the page refresh. If I refresh the page, the PHP executes and add the post_class according to my condition. And I can assume, somehow the global $post inside the project_add_favorite_class_to_post_class() is deceiving me.

It's also confirmed that, the single post (i.e. Post #646) classes generated by post_class() has it's relative post-646 class, where as the related posts have theirs, i.e. post-434, and none of them are same by any means.


I even tried modifying the foreach loop with setup_postdata( $post ), and passing $post->ID instead of passing $related_post_id where necessary inside the project_related_posts() function, but nothing happened.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.