• Resolved leiladesign

    (@leiladesign)


    Hello,

    I am working with your awesome plugin. I am not so expert with PHP and wordpress and I want to ask you something:

    I created this new metabox with the options_cb

    function cmb2_guide() {
        $prefix = '_cmb_';
    
        $cmb = new_cmb2_box( array(
            'id'            => 'categorie_guide_box',
            'title'         => __( 'Categorie Guide', 'cmb2' ),
            'object_types'  => array( 'guide_products', ), // Post type
            'context'       => 'side',
            'priority'      => 'low',
            'show_names'    => true,
        ) );
    
        $cmb->add_field( array(
        'id'         => $prefix . 'categorie_guide',
        'type'       => 'multicheck',
        'options_cb' => 'get_categorie_catalogo_guide',
    	) );
    }

    and this is my options_cb function:

    function get_categorie_catalogo_guide( $query_args ) {
        $args = wp_parse_args( $query_args, array(
            'post_type'   => 'catalogo-guide',
            'numberposts' => -1,
        ) );
    
        $posts = get_posts( $args );
    
        $post_options = array();
        if ( $posts ) {
            foreach ( $posts as $post ) {
              $post_options[ $post->ID ] = $post->post_title;
            }
        }
    
        return $post_options;
    }

    Now, I have some issues for the front-end part.
    Here’s my code in the php page where I need to show the elements:

    <?php
    $productCat = $post->post_name;
    
    	$args = array(
    		'post_type' =>  'guide_products',
    		'posts_per_page' => 50,
    		'order' => 'ASC'
    	);
    
    	$productQuery = new WP_Query( $args );
    ?>
    <?php if($productQuery->have_posts()) : ?><?php while($productQuery->have_posts()) : $productQuery->the_post(); ?>

    Now, I need to show all the ‘guide_products’ posts with the $productCat.
    I tried to add this option right after ‘order’ but it doesn’t work

    'meta_query' => array(
    			array(
    				'key' => '_cmb_categorie_guide',
    				'value' => $productCat
                 )
             ),

    How should I set it to work correctly?

    Thank you so much.

    https://wordpress.org/plugins/cmb2/

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

    (@tw2113)

    The BenchPresser

    From the looks of things, you’re storing post ID/Title pairs of the catalogo-guide post type, as post meta, using the meta key of _cmb_categorie_guide_box on posts of the guide_products post type.

    What I’m not seeing, at least with the code presented so far, is where $productCat is being set.

    Also not completely sure what you’re intending to do with the post IDs/titles you’re storing as post meta. Is that the part you’re intending to do a WP_Query on to display data based on?

    Thread Starter leiladesign

    (@leiladesign)

    I will try to explain what I need to do:

    I have two custom-post-types:
    1. Categories
    2. Products

    I used your plugin to create a multicheck for my products to be able to assign my CPT products to one (or more) of my CPT categories.

    Now, what I need to show, in the front-end is a WP_Query for each CPT category, so, example:

    CPT Categories:
    a) Category A
    b) Category B
    c) Category C

    In each page, I need to show my CPT products assigned to the category.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    It sounds like for the CPT Categories part, just having that have an archive, and using that archive link to list out each “category” post would suffice, and then when visiting a single post, say “Category A”, then in the single post template for that post type, do a custom WP_Query, using the IDs stored as post meta on the category post type, to grab post objects for the product post types. You wouldn’t need to deal with a meta query at all here, but you would have at least one extra WP_Query call to make.

    I hope I’m making sense, and I realize I’m answering more about the display of the content than the saving of the data. At bare minimum, this is what I’d end up doing.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom multicheck with options_cb’ is closed to new replies.