Forum Replies Created

Viewing 8 replies - 1 through 8 (of 8 total)
  • I also use flatsome and having the problem. Any fix to this?

    Thread Starter mustafamsy

    (@mustafamsy)

    @rockfire That’s what I actually was looking for, thanks a lot!

    This code did the trick for me

    function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
    	if ( $object_type !== 'unknown' || strpos( $uri, 'app_data/v1/data' ) === false ) {
    		return $object_type;
    	}
    
    	return 'ads';
    }
    add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
    

    I just want that endpoint to be flushed when any post is edited/added, so I just changed it to
    return 'post'; instead of return 'ads';

    • This reply was modified 4 years, 5 months ago by mustafamsy.
    Thread Starter mustafamsy

    (@mustafamsy)

    @rockfire I checked but the content is not flushing automatically. You can check from the URL I sent to your email that in recent_post: the last entry date is 9 Sept. while there are many new posts added after that one.

    Thread Starter mustafamsy

    (@mustafamsy)

    Hi @rockfire

    I have added type as you suggested but I am still see object type as unknown.

    Thread Starter mustafamsy

    (@mustafamsy)

    Sure, I have shared it to your the email.

    Thread Starter mustafamsy

    (@mustafamsy)

    @rockfire It is working fine except for auto flushing. The Object Type for my endpoint is shown as unknown.

    Thread Starter mustafamsy

    (@mustafamsy)

    Hi @rockfire

    Yes, I created a custom endpoint for one post type and other settings data that I am getting from another post using:

    function get_last_posts_with_thumbnail( $data ) {
        
         $query_ads = array(
            'post_type' => 'ads',
            'post_status' => 'publish',
        );
        
        $ads_post = get_posts($query_ads);
        
        for($v = 0; $v < count($ads_post); ++$v) {
            $ads_post[$v] = $ads_post[$v];
    
            $perm = get_permalink($ads_post[$v]->ID);
        	$thumbnail_id = get_post_thumbnail_id( $ads_post[$v]->ID );
        	$thumbnail = wp_get_attachment_image_src( $thumbnail_id, 'full');
    
            $ads_post[$v]->link = $perm;
            $ads_post[$v]->thumbnail = $thumbnail[0];
        }
    
        $settings_page = get_post('90201');
        
        $acf = get_fields($settings_page->ID);
        $settings_page->acf = $acf;
    
      return array (
          'ads'             => $ads_post,
          'settings'        => $settings_page,
        );
    
    }
    

    Above code is working. Then I registered this endpoint using:

    add_action( 'rest_api_init', function () {
      register_rest_route( 'app_data/v1', '/data/', array(
        'methods' => 'GET',
        'callback' => 'get_last_posts_with_thumbnail',
      ) );
    } );
    

    I enabled cache for this endpoint using:

    /**
     * Register the /wp-json/app_data/v1/posts endpoint so it will be cached.
     */
    function wprc_add_get_last_posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'app_data/v1' ] ) || ! in_array( 'data', $allowed_endpoints[ 'app_data/v1' ] ) ) {
            $allowed_endpoints[ 'app_data/v1' ][] = 'data';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_get_last_posts_endpoint', 10, 1);
    

    The cache is working for this endpoint but no auto flush upon post edit/publish.

    My request is that I want to be able to enable the auto flushing for this endpoint cache when I edit or publish a post.

    • This reply was modified 4 years, 6 months ago by mustafamsy.
    Thread Starter mustafamsy

    (@mustafamsy)

    Issue is resolved..

    My custom theme did not have the following in the header:

    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />

Viewing 8 replies - 1 through 8 (of 8 total)