Skip Navigation

[Closed] Query custom post by repeatable field filter

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.

This topic contains 6 replies, has 2 voices.

Last updated by Christopher Amirian 4 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2707717
Screenshot 2024-07-14 at 2.59.31 PM.png

Dear Sir/Madam,

Refer to the screenshot, this is a custom post 'booking' including repeatable field timeslot, each timeslot include more than one field like application id and timeslot status, I want to query the booking with the the application id is not empty and the timeslot status is reserved. Is it possible to have a query args and I query the booking post using get_posts($args)?

Best regards,

Kelvin.

#2707734

Christopher Amirian
Supporter

Languages: English (English )

Hi Kelvin

The Toolset Repeatable Field Groups are based on one-to-many relationships, so each item of Reapitable Field Groups is also a single post. Toolset does not allow to filter two related post types at the same time. You will have to filter the generated custom post type for your repeatable field. Then you will have to display their parent post(Reps) instead.

For more information:

https://toolset.com/forums/topic/searching-repeatable-fields/

https://toolset.com/forums/topic/filtering-a-view-by-a-date-field-in-a-repeating-field-group/

Thanks.

#2707785
timeslot.jpg

Dear Christopher,

this is the snippet code I used to query the child

$rfg_ids = toolset_get_related_posts( $booking->ID, 'timeslot', 'parent', 100, 0, array(), 'post_id', 'child');

May I know what I can do in the array(), I have the application-id, and timeslot-status in the repeatable field timeslot. How can I put the condition to application-id is empty or timeslot-status is reserved ?

#2708198

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Please consult with this documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

There are many options that you can add inside the array.

The args option can be used and inside it you can use like any other meta query of WordPress:

https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

Thanks.

#2708389

Dear Christopher,

I review the article https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters, I know how to query using meta_query but normally it requires passing the post id of the repeatable post.

Refer to below code

$rfg_ids = toolset_get_related_posts( $booking->ID, 'timeslot', 'parent', 100, 0, array(), 'post_id', 'child');

I will get the list of child post (the repeatable post) id, what if I set the array() in this code like

array( 'key' => 'wpcf-application-id', 'value' => '', 'compare' => '!=')

Will it automatically pass the child post id and query the post meat 'wpcf-applicataion-id' is empty?

#2708392

Dear Christopher,

Below is my final code

	$args = [
		'post_type' => 'booking',
		'p' => $booking_id
	];
		$reservation_ids = toolset_get_related_posts( $booking_id, 'timeslot', 'parent', 999, 0, array(), 'post_id', 'child');
		print_r($reservation_ids);
		foreach ($reservation_ids as $reservation_id) {
			printf("<br>Timeslot ID: %s ", $reservation_id);
			printf("Application ID: %s", get_post_meta($reservation_id, 'wpcf-application-id', true));
		}

I got result as below

Timeslot ID: 389 Application ID: 100
Timeslot ID: 390 Application ID:
Timeslot ID: 391 Application ID:
Timeslot ID: 392 Application ID:
Timeslot ID: 393 Application ID:
Timeslot ID: 394 Application ID:
Timeslot ID: 395 Application ID:
Timeslot ID: 396 Application ID:
Timeslot ID: 397 Application ID:
Timeslot ID: 398 Application ID:
Timeslot ID: 399 Application ID:
Timeslot ID: 400 Application ID:
Timeslot ID: 401 Application ID:
Timeslot ID: 402 Application ID:
Timeslot ID: 403 Application ID:
Timeslot ID: 404 Application ID:

I want to query those application id contain value, I change the code as below

	$post_meta = [
		'meta_key' => 'wpcf-application-id',
		'meta_value' => '',
		'meta_compare' => '!='
	];
	
	$bookings = get_posts($args);
	if ($bookings) {
		$reservation_ids = toolset_get_related_posts( $booking_id, 'timeslot', 'parent', 999, 0, $post_meta, 'post_id', 'child');
		print_r($reservation_ids);
		foreach ($reservation_ids as $reservation_id) {
			printf("<br>Timeslot ID: %s ", $reservation_id);
			printf("Application ID: %s", get_post_meta($reservation_id, 'wpcf-application-id', true));
		}
	}

It returns result as

Array()

How Toolset handle the meta key if value is empty?

#2708540

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

It is very hard to go over your code line by line to be able to help as it is way outside of our support scope as it falls under customization and coding.

You are welcome to hire a developer:

https://toolset.com/contractors/

From my understanding, there are two points that needs to be considered:

1. You constructed the meta query directly with meta_key, .... I suggest that you wrap that in a meta_query array.

2. You pass $post_meta directly as an argument, but you need to pass it as an args inside the array.

So maybe something like this:

$post_meta = [
    'meta_query' => [
        [
            'key' => 'wpcf-application-id',
            'value' => '',
            'compare' => '!='
        ]
    ]
];

$args = [
    'post_type' => 'booking',
    'p' => $booking_id
];

$bookings = get_posts($args);
if ($bookings) {
    $reservation_ids = toolset_get_related_posts(
        $booking_id,       // The ID of the booking post
        'timeslot',        // The relationship slug
        [                  // Arguments array
            'query_by_role' => 'parent',
            'args' => $post_meta, // Include the meta query here
            'limit' => 999,
            'offset' => 0,
            'return' => 'post_id',
            'role_to_return' => 'child'
        ]
    );

    print_r($reservation_ids);
    foreach ($reservation_ids as $reservation_id) {
        printf("<br>Timeslot ID: %s ", $reservation_id);
        printf("Application ID: %s", get_post_meta($reservation_id, 'wpcf-application-id', true));
    }
}

Please consider that I did not test the code above and it is just a suggestion for your further investigation.

Hope it helps.

The topic ‘[Closed] Query custom post by repeatable field filter’ is closed to new replies.