Beaver to consider another post in the loop

Hi,

I’m trying to override the current post in the loop.

For example:

global $post
 $post = get_post_id(1); // some post id - predefined page
 setup_postdata( $post );

I want to “cheat” Beaver (the whole WP) and make it render the post_id provided above (in the example id=1) with the layout scripts of post_id=1 that are added by:
add_action(‘wp_enqueue_scripts’, ‘FLBuilder::layout_styles_scripts’);
add_action(‘wp_enqueue_scripts’, ‘FLBuilder::styles_scripts’);

Beaver will render post=1 but when rendering the modules.
The custom modules have an indication of which data to render dynamically.

This is necessary for a problem I am trying to solve.
How do I achieve this?

I tried the following:

function set_page_template() {
    global $post;
    $post = get_post( 1 );
    setup_postdata( $post );
}
add_action('init', 'set_page_template');

But still, Beaver renders different POST_ID_layout.css
<link rel=‘stylesheet’ id=‘fl-builder-layout-289-css’ href=’…/wp-content/uploads/bb-plugin/cache/289-layout.css?ver=98a27aba73f876ddbe794159c49fe08c’ type=‘text/css’ media=‘all’ />

Instead of layout-1-css, ideas?

Cheers

I see the problem as follows:

FLBuilder::enqueue_layout_styles_scripts($post_id) accepts $post_id, however, it does not use it to load the provided $post_id related styles.

It uses another method to get the assets of the current post:

$asset_info = FLBuilderModel::get_asset_info();


// then loads the css file

wp_enqueue_style('fl-builder-layout-' . $post_id, $asset_info['css_url'], array(), $asset_ver);

In other words:

My goal is when the product page is accessed, to override the current post with a predefined post so the latter is loaded.

For example using www.SITE.COM/?product=my_product to be handled as if ?post=1 was given (predefined post with id = 1).

It feels to me that I need to override $wp_query at early stages because the GLOBALS[‘post’] is initialized using the value of $wp_query as in the following diagram:

Telling me how WooCommerce handles the get parameter ?product=my_product to determine the active page would help or give ideas. I cannot find out how WC does that.

Hey Elemento,

I have already assigned this to another member of the team who can answer your inquiry.

Ben

Hi Elemento,

Beaver Builder works off of the main WordPress loop, so I’m not entirely sure if what you are trying to do is possible.

If it is, it sounds like you may need to try overriding it earlier so the builder’s methods are using the post info that you have intended.

Have you looked into the pre_get_posts filter? I believe that should allow you to modify the main query before it is run. Let me know if that works.

Justin

Yes, currently that’s what I am trying to do.

I printed the $query object inside the action. I understand that the system creates a query that depends on the URL. Now I am looking on how to override the current post. After calling it with high priority, and try to print $query, this is what I get.

Now I want to alter the current post to another predefined one (e.g. post with ID=1)


    [query] => Array
        (
            [product] => elm_sample_product
            [post_type] => product
            [name] => elm_sample_product
        )

    [query_vars] => Array
        (
            [product] => elm_sample_product
            [post_type] => product
            [name] => elm_sample_product
            [error] => 
            [m] => 

It would be great if there is a way to do that without affecting WC globals, but that’s easy to solve.

I know it is now out of Beaver scope, but would be great if you provide assistance.
This is important for my Beaver because I am planning to make WooCommerce pages using Beaver D&D.

Hi Elemento,

Unfortunately, this is beyond the scope of the support we offer here as I would really need to spend a significant amount of time researching and writing code for you to get this to work.

I don’t mind pointing people in the right direction for help with custom code, but in this case, you are trying to do something that I’m not really that familiar with.

I can tell you that the builder uses the post ID that is either provided in the global $post variable, or in data that is sent to the server while you are working on a page. You can see that in FLBuilderModel::get_post_id(). The method get_asset_info uses that same post ID, so it does sound like you’re still not completely overriding it.

I would do some research on overriding the main query using pre_get_posts as from the brief reading I did, it sounds like that’s what you need.

I hope that helps.

Thanks,
Justin