This topic contains 13 replies, has 2 voices, and was last updated by  dhuy 1 month, 2 weeks ago.

  • Creator
    Topic
  • #11595
    Resolved
    dhuy
    Participant

    Hi,
    I’ve created a CPT for pricing tables and I want to show it in a page using the post field.
    Currently, I’m stuck at how to get the CPT id to display the pricing table.

    $table = isset( $pricing['pricing_table_id'] ) ? $pricing['pricing_table_id'] : '';
                      if( $table ) {
                        setup_postdata( $GLOBALS['post'] =& $table );
                        $plans = rwmb_meta( 'plan' );
                        if( ! empty( $plans ) ) {
                          foreach ($plans as $plan) {
                            $name = isset( $plan['plan_name'] ) ? $plan['plan_name'] : '';
                            echo $name;
                          }
                        }
                        wp_reset_postdata();
                      }
Viewing 10 replies - 1 through 10 (of 13 total)
  • Author
    Replies
  • #11604

    Anh Tran
    Keymaster

    Hi Huy,

    Do you mean getting the post object or getting the items in the pricing table?

    Can you also share the code of the meta box?

    #11627

    dhuy
    Participant

    Yes, the ultimate goal is getting meta boxes of the post object.

    #11629

    Anh Tran
    Keymaster

    Hi Huy,

    To the the current post object, please use:

    $post = get_post();

    To get fields, just use rwmb_meta(). But I think you already know that.

    I’m not sure what your problem is. Can you provide more details and the setup code of meta box, so I can give more useful instruction?

    #11630

    dhuy
    Participant

    Below is the meta box of the page builder. I can select the Pricing table (CPT) via the post field. I’m stuck at pulling the meta boxes from the selected CPT.

    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array (
            'title' => 'Page',
            'id' => 'page',
            'post_types' => array(
                'page',
            ),
            'context' => 'after_title',
            'priority' => 'high',
            'fields' => array(
                
                array (
                    'id' => 'section',
                    'type' => 'group',
                    'name' => 'Section builder',
                    'fields' => 
                    array (
                        0 => 
                        array (
                            'id' => 'section_pricing',
                            'type' => 'group',
                            'name' => 'Pricing',
                            'fields' => 
                            array (
                                0 => 
                                array (
                                    'id' => 'pricing_table_id',
                                    'type' => 'post',
                                    'name' => 'Post',
                                    'post_type' => 
                                    array (
                                        0 => 'prices',
                                    ),
                                    'field_type' => 'select_advanced',
                                ),
                            ),
                            'default_state' => 'collapsed',
                            'groupfield' => 'text',
                            'collapsible' => true,
                            'visible' => true
                        ),
                        
                    ),
                    'clone' => 1,
                    'sort_clone' => 1,
                    'default_state' => 'collapsed',
                    'collapsible' => true,
                    'save_state' => true,
                    'add_button' => 'Add section',
                ),
            ),
            'style' => 'seamless',
        );
        return $meta_boxes;
    }

    Meta boxes of CPT:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array (
            'title' => 'Pricing table',
            'id' => 'pricing-table',
            'post_types' => array(
                'prices',
            ),
            'context' => 'after_title',
            'priority' => 'high',
            'fields' => array(
                
                array (
                    'id' => 'plan',
                    'type' => 'group',
                    'name' => 'Group',
                    'fields' => 
                    array (
                        0 => 
                        array (
                            'id' => 'plan_class',
                            'type' => 'text',
                            'name' => 'Class',
                        ),
                        1 => 
                        array (
                            'id' => 'plan_name',
                            'type' => 'text',
                            'name' => 'Name',
                        ),                  
                    ),
                    'clone' => 1,
                    'default_state' => 'collapsed',
                    'collapsible' => true,
                    'add_button' => 'Add table',
                    'group_title' => 'Table',
                ),
            ),
            'style' => 'seamless',
        );
        return $meta_boxes;
    }
    #11637

    Anh Tran
    Keymaster

    I see, it’s much clearer now.

    To get the field values from your table CPT, please pass the post ID (value of the field pricing_table_id) to the 3rd parameter of rwmb_meta function. Here is a modified version of your initial code:

    $table = isset( $pricing['pricing_table_id'] ) ? $pricing['pricing_table_id'] : '';
    if ( $table ) {
      $plans = rwmb_meta( 'plan', '', $table ); // THIS
      if ( ! empty( $plans ) ) {
        foreach ($plans as $plan) {
          $name = isset( $plan['plan_name'] ) ? $plan['plan_name'] : '';
          echo $name;
        }
      }
    }
    #11640

    dhuy
    Participant

    Thanks for your swift respond.
    Strangely, it still doesn’t work.

    #11641

    dhuy
    Participant

    At last, it’s OK now.

    #11642

    Anh Tran
    Keymaster

    Have you resolved this? Do you need any further help?

    #11645

    dhuy
    Participant

    It works like a charm!
    Another question, I’m working with template page.php, in which there are many modules such as pricing tables, FAQs, features,… For better organization, I put each module in separate files which are located in different folder. Then, in the page.php, I just call the function for respective module.
    Problem is value in fields cannot be retrieved, all none whilst html output is OK. Please correct me if missed anything.
    Image

    #11656

    Anh Tran
    Keymaster

    For better organization, I put each module in separate files which are located in different folder.

    This is great and is a recommended way to organizing fields. I also wrote about this.

    Problem is value in fields cannot be retrieved, all none whilst html output is OK.

    Can you provide more details on how you retrieve values? Code is appreciated.

Viewing 10 replies - 1 through 10 (of 13 total)

You must be logged in to reply to this topic.