• I want to display 3 posts with their values pre-filled on the front end. All 3 posts are of the same type and have the same meta box id. When I call cmb2_metabox_form 3 times, every time providing the id of the post, the first post that I provide is duplicated 3 times

    e.g.
    cmb2_metabox_form(“mytype”, 1);
    cmb2_metabox_form(“mytype”, 2);
    cmb2_metabox_form(“mytype”, 3);

    The output is the form corresponding to post id 1 repeated 3 times. Is there some sort of caching at work?

    All 3 ids exist. I’m using CMB2 in wordpress.

    https://www.ads-software.com/plugins/cmb2/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Without actually having your code to see, I wager you’re needing to separate out the saved content by ID. For example, my_form_1, my_form_2, etc instead of just my_form. It’s not going to know it needs to go to the next set of items, and thus just return the first set it finds each time.

    Hopefully that makes sense.

    Thread Starter contactashish13

    (@rozroz)

    So, I’m defining the meta boxes for a particular CPT “test” as below.

    $cmb = new_cmb2_box( array(
    		"id"            => "test",
    		"object_types"  => array("test"),
    		"context"       => "normal",
    		"priority"      => "high",
    		"show_names"    => true, // Show field names on the left
    	) );

    On the front end, I’m iterating through a list of post IDs and extracting the form using the below code

    $postIDs    = array(1, 2, 3);
    $forms      = array();
    foreach($postIDs as $postID){
        $form       = cmb2_metabox_form("test", $postID, array( "cmb_styles" => false, "form_format" => "", "echo" => ""));
        $forms[]    = $form;
    }
    print_r($forms);

    The output array contains the form for postID 1 repeated 3 times. If I change the $postIDs array to (2, 3, 1) then the output is for postID 2 repeated 3 times.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Upon further testing and inspection, I believe it’s related to output buffering.

    Specifically https://cmb2.io/api/source-function-cmb2_get_metabox_form.html#208 which gets reached as you progress through the form. It receives the right IDs and whatnot, but the ob_get_contents seems to returning the previous version and not the new one.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thread Starter contactashish13

    (@rozroz)

    Thanks. I already raised an issue but came here in the hope that this was not a bug but a misunderstanding at my end.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Caching/duplication when calling cmb2_metabox_form’ is closed to new replies.