Skip Navigation

[Resolved] Query repeatable field with specific key value

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Minesh 1 year, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2575681
Screenshot 2023-03-18 at 12.17.20 PM.png

Dear Sir/Madam,

I have a custom post type ' coupon' containing repeatable field, there are 4 posts to the repeatable field group, I want to have a shortcode to query only the status is either used or noshow. Below is my script but it can't filter the post, I show all.

<?php
    define( 'WP_USE_THEMES', false );
    require_once '../wp-load.php' ;
    
    $post_id = 4758;
    $args = array(
	        'key' => 'wpcf-book-status',
	        'value' => array('used', 'noshow'),
	        'compare' => 'IN',
    );	
    $rfg_ids = toolset_get_related_posts( $post_id, 'booking-group', 'parent', 100, 0, $args, 'post_id', 'child');
    print_r($rfg_ids);
    printf("Total found: %s<br/>", count($rfg_ids));
    if ( $rfg_ids ){
        foreach ( $rfg_ids as $rfg_id) {
            $book_time = get_post_meta( $rfg_id, 'wpcf-book-timestamp', true);
            $status = get_post_meta( $rfg_id, 'wpcf-book-status', true);
            printf("%s - %s<br/>",date('Y-m-d H:i:s', $book_time), $status);
        }
    }		
#2575715

I tried below code but not work

		$args = array(
			'meta_query' => array(
				array(
					'key' => 'wpcf-book-status',
					'value' => array('used', 'noshow'),
					'compare' => 'IN'
				),
			)
    );	

Below code not work if compare is IN, it return 0 result

		$args = array(
			'meta_key' => 'wpcf-book-status',
			'meta_value' => array('used', 'noshow'),
			'meta_compare' => 'IN'
    );	

Only compare is = can work

		$args = array(
			'meta_key' => 'wpcf-book-status',
			'meta_value' => 'used',
			'meta_compare' => '='
    );
#2576833

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As you can see with our official Doc for "toolset_get_related_posts":
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts


4. array args: Additional query arguments to narrow down the search. Accepted arguments:
meta_key, meta_value and meta_compare: Works exactly like in WP_Query. Only limited values are supported for meta_compare ('=' | 'LIKE').

The only possible comparisons are ('=' | 'LIKE').

So what if you try to build the argument like:

 $args = array(
        'meta_query' => array(  
            'relation' => 'OR',
            array(
                'key' => 'wpcf-book-status',
                'value' => 'used',
                'compare' => '='
            ),array(
                'key' => 'wpcf-book-status',
                'value' => 'noshow',
                'compare' => '='
            ),
        )
);  

Does that helps?