Tell us what you are trying to do?
I have made a repeatable field group in posts for being able to select related posts.
To being able to select other posts I use this code (found on your website):
//dynamically populate select field from Types
add_filter( 'wpt_field_options', 'prefix_custom_options', 10, 3);
function prefix_custom_options( $options, $title, $type ){
switch( $title ){
case 'Related posts':
$options = array();
$args = array(
'post_type' => 'post',
'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;
}
I have made a view to show the selected posts in the RFG, but they online show the title (if we typed it by hand in the RFG) or a number (I think it's the post ID) and a url, ending with .../related/rfg-4/ instead of a working url (actual url of the post).
I would like to show the title of the post and the correct url (which can become a link) in the view, but I don't know how to edit the above code to get the correct data.
Is there any documentation that you are following?
https://toolset.com/forums/topic/dynamically-populate-select-field/
What is the link to your site?
It's our accept environment, behind a firewall. I can't publish it online.
Hi,
Thank you for contacting us and I'd be happy to assist.
When populating options of the custom field dynamically using the 'wpt_field_options', as shown in your code, it stores the selected post's ID as the raw custom field value.
Assuming that the slug of your custom field is 'related-posts', that post ID can be accessed using the Types Fields API shortcode:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ )
[types field='related-posts' output='raw'][/types]
And you can then use this post ID, in the "wpv-post-link" shortcode to generate a post link with the title, through the 'item' attribute:
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-link )
[wpv-post-link item='[types field='related-posts' output='raw'][/types]']
Or to get the URL of that post, you can use the 'wpv-post-url' shortcode:
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-url )
[wpv-post-url item='[types field='related-posts' output='raw'][/types]']
I hope this helps and please let me know if you need any further assistance with this.
regards,
Waqar
My issue is resolved now. Thank you!