All sites broken with the last update to getting post id.

Hi,

We use Beaver for all our clients to create smart E-Commerce drag and drop feature using the power of Beaver.

With the last update, seemingly you changed the logic of getting the current post ID and thus all of our clients are now broken, all of them.

  • Fixed a bug with getting the correct ID for the current post.
  • What we used to do is simple, we have a static page template (page created with Beaver, let’s assume its ID=40) and for pages with a certain meta option (e.g. “load_template_instead” set to true) we load the page 40 instead of it.

    This is the display template:

            global $post;
            $curr_post = $post;
            $post = get_post( 40 );
            the_content(); 
            $post = $curr_post;
    

    And this is the ‘fl_builder_global_posts’ hook in order to load styling of page 40:

        public function wc_product_page_load_fl_template( $post_ids ) {
            if ( !Helper::load_template_instead() ) 
                return $post_ids;
            
            $post_ids[] = 40;
            return $post_ids;
        }
    
    

    All we want to do is to load a static Beaver page instead of the current one.
    The way we did it above, FLBuilder::render_content following line was equal to 40

    		$post_id = FLBuilderModel::get_post_id();
    
    

    But now it is the POST_ID of the current page, thus rending the wrong content.

    Note, we don’t use secondary loops because there are global variables that might be lost (e.g. global $product of WooCommerce plugin).

    What can we do in order to solve this issue and make rending custom page an option?
    This is our bread and butter and thus why we have been working with Beaver for a long time.

    Please, help us our sites are broken.

    Hey Elemento,

    Sorry for the issues. We had to change the logic of FLBuilderModel::get_post_id as it is intended to return the post ID for the current post in the main loop. Secondary loops that weren’t correctly resetting the global post data were breaking the builder, so we made the change to fix that.

    I’m not quite understanding where your issue is, can you elaborate on what exactly isn’t working? Is it your wc_product_page_load_fl_template function? Or do you need FLBuilderModel::get_post_id to return the post ID of your template?

    Justin

    I need FLBuilderModel::get_post_id to return my template ID. Exactly.
    See my comment below.

    		// Get a post ID sent in an AJAX request.
    		if ( isset( $post_data['post_id'] ) ) {
    			 return $post_data['post_id'];
    		}
    // Get a post ID from the main query.
    // ** THIS NEW CONDITION IS NOW TRUE THUS RETURNING THE "REAL" ID **
    		else if ( in_the_loop() && is_main_query() && isset( $wp_the_query->post ) ) {
    		 return $wp_the_query->post->ID;
    		}
    		// Get a post ID in a query outside of the main loop.
    		else if ( isset( $post ) ) {
    			 return $post->ID;
    		}
    

    Unfortunately, we need FLBuilderModel::get_post_id to return the “real” ID. If it doesn’t, third party shortcodes/widgets can break the builder.

    You might try this…

    global $wp_the_query;
    $curr_post = $wp_the_query->post;
    $wp_the_query->post = get_post( 40 );
    the_content(); 
    $wp_the_query->post = $curr_post;
    

    Another way that might work would be…

    global $post;
    $post = get_post( 40 );
    setup_postdata( $post );
    the_content();
    wp_reset_postdata();
    

    Let me know how that goes.

    Justin

    Thanks for the suggestions, I will try them.

    I got this working solution too:

        add_filter( 'the_content', array(&$this, 'set_post_id_for_template' ), 1 );
    
        public function set_post_id_for_template( $content ) {
            if ( ! should_load_template() )
                return $content;
            
            $_POST['post_id'] = 40;
            return $content;
        }
    

    Just in case your solutions (that look neater tbh) fail, what are the main usage of the following condition:

    		if ( isset( $post_data['post_id'] ) ) {
    			 return $post_data['post_id'];
    		}
    
    

    What are the risks?

    Just in case your solutions (that look neater tbh) fail, what are the main usage of the following condition:

    The $post_data array is set either by the $_POST[‘fl_builder_data’] JSON sent via AJAX, or by the $_POST array itself. You can see that in FLBuilderModel::get_post_data.

    What are the risks?

    It’s hard to say if there are any. Give it a shot and let me know how it goes.

    Justin

    The $post_data array is set either by the $_POST[‘fl_builder_data’] JSON sent via AJAX

    That only happens when the editing mode is active, in contrast to the loading site. Correct?

    `global $wp_the_query; $curr_post = $wp_the_query->post; $wp_the_query->post = get_post( 40 ); the_content(); $wp_the_query->post = $curr_post;`

    That seems to work perfectly too.

    Thanks for your fast support =)

    That only happens when the editing mode is active, in contrast to the loading site. Correct?

    That is correct.

    That seems to work perfectly too.

    Great! You’re welcome! :slight_smile: