Skip Navigation

[Resolved] Wrong link for item in repeatable field group

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 Ron 1 year, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2502875

Ron

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.

#2503683

Waqar
Supporter

Languages: English (English )

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

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

#2505381

Ron

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.