Skip Navigation

[Resolved] Filter list of posts by a custom field value via REST API

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 1 reply, has 2 voices.

Last updated by Minesh 7 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2701101

Hi there,

I have two websites: hidden link and hidden link. I added a Toolset custom field on two.test, a checkbox associated with Posts.

On the homepage of one.test, I want to use the REST API to display all posts on two.test that have the checkbox ticked. I have written nearly all of the code to retrieve posts from two.test and display them on one.test. The only challenging part is retrieving only posts which have their checkbox ticked (the value of the checkbox custom field is "1" when ticked, "" when not ticked).

Can you assist me with this? Here's some of my code to give you a clearer picture:

		$rest_fields = [
			'title',
			'id',
			'link',
			'featured_media', // featured image ID only, not the image path
                       'toolset-meta.post-group.checkbox-custom-field.raw',
		] ;
		$rest_arguments = [
			'_fields'=> implode(',',$rest_fields),
			'per_page' => 20, // maximum number to retrieve
			'orderby' => 'modified', // date modified
			'order'=> 'desc',
		] ;
               $rest_url = add_query_arg ( $rest_arguments, '<em><u>hidden link</u></em>' ) );
		$curl_args = [
			'timeout' => 10,
			'headers' => [ 'Accept' => 'application/json', ]
		] ;
		$response = wp_remote_get ( $rest_url, $curl_args );
		if ( is_wp_error ( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
			return false ; 
		}
		$response = json_decode(wp_remote_retrieve_body( $response ), true);

Thank you.

Saul

#2701209

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Have you checked that Toolset offers REST API integration:
- https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/

Please check the following related tickets that might help you:
- https://toolset.com/forums/topic/custom-search-and-filter-with-rest-api/
- https://toolset.com/forums/topic/show-and-update-types-checkboxes-in-rest-api/

Maybe you will have to write the rest API endpoint to get the checkboxes values for the checkboxes options.
- https://stackoverflow.com/questions/53126137/wordpress-rest-api-custom-endpoint-with-url-parameter

Checkboxes are stored as serialize array so you will have to manipulate the serialize array for all checkboxes options.

#2701264

Hi Minesh,

Thanks for your response. These links were super helpful.

Take care!

Saul