[Hummingbird] Cache and Custom Post Fetch Issue

Hi Everyone!
I hope you guys are doing well and I continue to love this site and its services. Looking forward to seeing any new and amazing products you bring to us.
I have a question and it's been really bothering me. Not sure if it's related to the caching in Hummingbird but I have a custom code that retrieves the latest custom post type from an author. A short code was created and placed inside a tab in BuddyPress. Basically, a simple code was created to add a custom post type to the WP dashboard and then a post is published by a specific user/author. That post is privately published, and the author was assigned to that post. Then the shortcode just pulls the latest post from that author from that specific post type.
Here's the code used to create the custom post type:

/**
 * Register post type for User Content.
 */
function buddydev_register_post_type() {
	register_post_type( 'bp_user_content', array(
		'label'    => 'User Content',
		'labels'   => array( 'name' => 'User Contents' ),
		'public'   => false,
		'show_ui'  => true,
		'supports' => array( 'title', 'editor', 'author', 'custom-fields' )
	) );

	/* New post type*/
	register_post_type( 'post_type_name', array(
		'label'    => 'Label Post Type',
		'labels'   => array( 'name' => 'Plural Label' ),
		'public'   => false,
		'show_ui'  => true,
		'supports' => array( 'title', 'editor', 'author', 'custom-fields' )
	) );
}

add_action( 'init', 'buddydev_register_post_type' );

Here's the code used to fetch latest post type and it is on GitHub:

https://gist.github.com/sbrajesh/e932e6d8bbfd4a515e070f520ce40ef4

The problem I am having:
I created a custom tab on buddypress called 'Home' with its own subnav called 'Home'. In the content of that subnav, I have the following shortcode:
[bp-user-last-post post_type='custom post type name']
So, the 'custom post type name' will be replaced by any post type created. I am finding that most of the time the post 'fetching' works but other times it doesn't. Sometimes when users are logged in, it works but when users are logged out, it doesn't. This is why I am thinking maybe it is some caching problem.

Can someone take a look at the codes I provided and see if with your professional opinion, there may be something amiss or needs correcting?

Thank you!!
Sujee

  • Dimitris
    • Support Star

    Hello Sujee

    Please grant support access to your website via WPMUDEV Dashboard plugin as shown here
    https://premium.wpmudev.org/docs/getting-started/getting-support/#chapter-5
    so we can have a better look at your setup and HummingBird caching settings, and I'll then forward this to our developers to have a better look. :slight_smile:
    Let us know here in your next reply that access has been granted.

    Warm regards,
    Dimitris

  • Dimitris
    • Support Star

    Hello Sujee

    I'm sorry for the long waiting time here. It seems that support access isn't enabled at the moment.
    Could you please double check that and let us know here in your next reply?
    If you see in your end that's active, please try to revoke it and re-grant it.

    Warm regards,
    Dimitris

  • Sujee
    • Recruit

    Thank you Dimitris! I've added it again. I would really appreciate the help to identify what is going on. It seems like the tabs works sometimes and other times it does not. When users are logged in, it is more dependable.

    Thanks,
    Sujee

  • Dimitris
    • Support Star

    Hello there Sujee

    I just checked both above links as logged-in (using the granted support access) and non-logged-in user, but couldn't locate any change between them.
    The content inside the "Home" tab was the same for these users.
    I also had another colleague of mine to check this, to be sure that's not something in my end only.

    You've mentioned that "Sometimes when I clear cache or by random, it starts working again.", so I guess this is happening due to some cache that doesn't get cleared properly and I'm not able to experience that now.

    Can you please try to go to Page Caching settings in HummingBird, activate the Clear full cache when post/page is updated option and see if that makes any difference?

    Also, are you testing in the same browser window? I wonder if browser caching is messing with that also, so please do check in separate browsers the logged-in and not logged-in users.

    Meanwhile, I've forwarded this thread to HummingBird's lead developer to have a quick look. Please keep in mind that response time on this may be a bit longer though, hope you understand! :slight_smile:

    Warm regards,
    Dimitris

  • Sujee
    • Recruit

    Dimitris,

    Thank you! You should quickly go see these pages:

    collegetrackr.com/members/rollinscollege
    collegetrackr.com/members/moraviancollege

    I tried different browsers but still not working. I disabled Hummingbird and not working so I guess it isn't even that. I think there is something off with the code that I had provided earlier and can be found here: https://gist.github.com/sbrajesh/e932e6d8bbfd4a515e070f520ce40ef4

    Hope we can get his resolved soon.

    Thanks again!

    Sujee

  • Dimitris
    • Support Star

    Hello Sujee

    I was able to see the difference for the /rollinscollege member.
    After clearing Page Caching in HummingBird, I can see proper content as non-logged-in user.
    I went ahead and activated the Clear full cache when post/page is updated option in Page Caching, please let me know, if that makes any difference. :slight_smile:

    Warm regards,
    Dimitris

  • Dimitris
    • Support Star

    Could you please try to exclude all these URLs from getting cached from HummingBird's Page Caching? Simply go to HummingBird -> Caching -> Page Caching and insert a new line in the Exclusions area:
    \/members+\/[a-z]+\/home\/
    Then clear all caches and see if that makes any difference.

    Warm regards,
    Dimitris

  • Sujee
    • Recruit

    Hi Dimitris,

    Thank you for the advise. I have done as you asked but to no avail. I'm thinking maybe it's not the Hummingbird cache issue - it could be bad coding with what I had provided earlier. Not sure why some of them work and others don't even after clearing all cache.

    I will play around with it and see if I can somehow find a fix or may need to find a workaround solution.

    Hope you can continue working on it though - your patience and help is much appreciated!

    Sujee

  • Sujee
    • Recruit

    I just went to 'Advanced Tools' for hummingbird and did a database cleanup and deleted all transients and now none of the pages work. It's frustrating! I think if we can't get it done by tomorrow, I'll figure out another solution, but I really appreciate your help!! You guys continue to be awesome with your support.

    Sujee

  • Dimitris
    • Support Star

    Dear Sujee,

    appreciate your kind words here! :slight_smile:

    Please ensure that this isn't a strictly HummingBird issue, by completely deactivating all caching modules.

    Also, is there any other caching involved there? Anything that goes from server-level or any other 3rd party service like Cloudflare?

    Warm regards,
    Dimitris

  • Sujee
    • Recruit

    Dimitris,

    If you take a look at the code I use to create the Home pages, it does state it fetches transients so that explains why the tabs don't work when I do a database cleanup:

    Skip to content

    Search…
    All gists
    GitHub

    Instantly share code, notes, and snippets.

    0 0 @sbrajeshsbrajesh/wordpress-latest-post-content-fetch.php
    Created 20 days ago

    <script src="https://gist.github.com/sbrajesh/e932e6d8bbfd4a515e070f520ce40ef4.js"></script>

    Code Revisions 1
    Efficient WordPress Latest Post content fetch for a user
    wordpress-latest-post-content-fetch.php

    /**
     * Efficient fetch of Latest Post content of a user by post type.
     *
     * <a title=@param href=/profile/param>param</a> array $atts shortcode atts.
     * <a title=@param href=/profile/param>param</a> string $content n/a
     *
     * @return mixed|string|void
     */
    function buddydev_user_last_post_shortcode( $atts, $content = '' ) {
    	$atts = shortcode_atts( array(
    	        // by default, If BuddyPress is active, use displayed user.
    	        'user_id' => function_exists( 'bp_displayed_user_id' ) ? bp_displayed_user_id() : 0,
                'post_type' => 'post',
            ), $atts );
    
    	$user_id = $atts['user_id'];
    	if ( ! $user_id ) {
    		return $content;
    	}
    
    	$last_post_id = get_transient( "bp-user-lost-post-{$atts['post_type']}-{$user_id}" );
    	if ( ! $last_post_id ) {
    		$query = new WP_Query( array(
    			'author'         => $user_id,
    			'post_type'      => $atts['post_type'],
    			'posts_per_page' => 1,
    			'fields'         => 'ids',
    			'orderby'        => 'modified'
    		) );
    
    		if ( ! empty( $query->posts ) ) {
    			$last_post_id = array_pop( $query->posts );
    			set_transient( "bp-user-lost-post-{$atts['post_type']}-{$user_id}", $last_post_id, DAY_IN_SECONDS );
    		}
    	}
    
    	if ( ! $last_post_id ) {
    		return '';
    	}
    
    	$post = get_post( $last_post_id );
    	if ( ! $post ) {
    		return $content;
    	}
    
    	return apply_filters( 'the_content', $post->post_content );
    }
    
    add_shortcode( 'bp-user-last-post', 'buddydev_user_last_post_shortcode' );
    
    /**
     * Clear transient on New Post, trash post, delete post.
     *
     * <a title=@param href=/profile/param>param</a> int $post_id post id.
     */
    function buddydev_clear_last_post_transient( $post_id ) {
    	if ( ! defined( 'DOING_AUTOSAVE' ) ) {
    		$post = get_post( $post_id );
    		delete_transient( "bp-user-lost-post-{$post->post_type}-{$post->post_author}" );
    	}
    }
    
    // on new post or delete post clear transient.
    add_action( 'save_post', 'buddydev_clear_last_post_transient' );
    add_action( 'wp_trash_post', 'buddydev_clear_last_post_transient' );
    add_action( 'before_delete_post', 'buddydev_clear_last_post_transient' );
  • Dimitris
    • Support Star

    No need to deactivate whole HummingBird plugin, just the Caching modules from it.

    HummingBird's CDN is for serving your optimised assets (CSS and JS files). If deactivating Caching doesn't make any difference, please try to deactivate Asset Optimisation too (please take notice of your settings there, so you could easily re-enter them, if needed).

    Warm regards,
    Dimitris

  • Sujee
    • Recruit

    FYI - when I tried reactivating page cache, I got this message but it went away after I cleared cache:

    Hummingbird could not locate the WP_CACHE constant in wp-config.php file for WordPress. Please make sure the following line is added to the file:
    define('WP_CACHE', true);

  • Dimitris
    • Support Star

    Hello there Sujee,

    hope you're doing good today! :slight_smile:

    As this isn't a issue around HummingBird, I can't forward it to its lead developer, I do escalated it though to our Second Level Support in order to have a better look at your code.
    Please keep in mind that their response times are a bit wider than ours here in forums, your patience is highly appreciated!

    Warm regards,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.