Query posts by subfield

Support MB Group Query posts by subfield

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #4447
    isuberoisubero
    Participant

    Hi! Loving metabox! I need to use get_posts() (or some alternative) to query posts by subfield. Let's take as an example the very same code from the documentation:

    
    add_filter( 'rwmb_meta_boxes', 'meta_box_group_demo_register' );
    function meta_box_group_demo_register( $meta_boxes ) {
    	$meta_boxes[] = array(
    		'title'  => __( 'Album Tracks' ),
    		'fields' => array(
    			array(
    				'id'     => 'standard',
    				// Group field
    				'type'   => 'group',
    				// Clone whole group?
    				'clone'  => true,
    				// Drag and drop clones to reorder them?
    				'sort_clone' => true,
    				// Sub-fields
    				'fields' => array(
    					array(
    						'name' => __( 'Track name', 'rwmb' ),
    						'id'   => 'text',
    						'type' => 'text',
    					),
    					array(
    						'name' => __( 'Release Date', 'rwmb' ),
    						'id'   => 'date',
    						'type' => 'date',
    					),
    					array(
    						'name'    => __( 'Genre', 'rwmb' ),
    						'id'      => 'genre',
    						'type'    => 'select_advanced',
    						'options' => array(
    							'pop'  => __( 'Pop', 'rwmb' ),
    							'rock' => __( 'Rock', 'rwmb' ),
    						),
    					),
    				),
    			),
    		),
    	);
    	return $meta_boxes;
    }
    

    How can I query posts by 'genre' or any other field?

    #4454
    Anh TranAnh Tran
    Keymaster

    There's no direct way to query post by sub-fields of a group. Because the data is stored as a serialized array in the post meta. You might want to refer to some techniques to solve this:

    https://support.metabox.io/topic/reverse-query-on-clonable-post-metabox/
    https://support.metabox.io/topic/new-field-class-hooking-to-rwmb_after_save_post/
    https://support.metabox.io/topic/use-repeatable-meta-box-field-for-query-orderby/

    #4477
    isuberoisubero
    Participant

    Alright, thanks!

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Query posts by subfield’ is closed to new replies.