Skip Navigation

[Resolved] Display posts using values from dynamically populated select custom fields

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by hollyR-3 3 months, 2 weeks ago.

Assigned support staff: Waqar.

Author
Posts
#2557055

Tell us what you are trying to do?
I have a custom post type (locations) that uses a content template. Inside the content template, I want to create a view that lists 3 nearby locations. The locations are the same post type, kind of like related posts but author defined.

I'm using 3 select custom fields (Nearby Communities One, Nearby Communities Two, Nearby Communities Three) in the locations post type dynamically populated from the locations post list using this code:

add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);

function func_dynamic_populate( $options, $title, $type ){
switch( $title ){
case 'Nearby Communities One':
case 'Nearby Communities Two':
case 'Nearby Communities Three':

$options = array();
$args = array(
'post_type' => 'location',
'posts_per_page' => -1,
'post_status' => 'publish');

$posts_array = get_posts( $args );

foreach ($posts_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->post_title,
);
}
break;
}
return $options;

This code works to populate the select fields so the author can choose their 3 nearby location posts.

I've created a view to display these posts in the content template, but I can't work out what the query filter in the view should look like. No matter what I do the view just shows the latest 3 posts rather than the ones defined in the select fields.

Is there any documentation that you are following?
Nope, can't find anything

Is there a similar example that we can see?
No

What is the link to your site?
hidden link <<example page, Nearby Communities section

#2557849

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To limit the view's results, to only the location post IDs, coming from those 3 custom fields, you can use the 'wpv_filter_query' filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example:


add_filter( 'wpv_filter_query', 'custom_filter_custom_field_posts', 1000 , 3 );
function custom_filter_custom_field_posts( $query_args, $view_settings ) {
	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 7547) ) {

		$post_array = array();
		$post_array[] = types_render_field( 'nearby-communities-one', array( 'output' => 'raw' ) );
		$post_array[] = types_render_field( 'nearby-communities-two', array( 'output' => 'raw' ) );
		$post_array[] = types_render_field( 'nearby-communities-three', array( 'output' => 'raw' ) );

		$query_args['post__in'] = $post_array;
		
	}
	return $query_args;
}

Note: The '7547' is the ID of the view that is being used on that example page, for the nearby location. Please make sure that the custom field slugs 'nearby-communities-one', 'nearby-communities-two', & 'nearby-communities-three' are used as set on your website.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2557891

My issue is resolved now. Thank you!