Skip Navigation

[Resolved] Dropdown of post titles, saving post id

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to add drop-down with post title as select option with CRED form

Solution:
You should use CRED Generic field to build dropdown with your custom post title where the source of dropdown option would be the view.

You can find proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/dropdown-of-post-titles-saving-post-id/#post-609343

Relevant Documentation:

This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

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

Last updated by gavinS-2 6 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#609246

Tell us what you are trying to do?

I am trying to create a dropdown on a CRED form that displays a list of post titles (Post type: Item), and saves the post id to a field in another post type (Post type: Accessories)

Is there any documentation that you are following?

I am trying to follow the instructions here: https://toolset.com/forums/topic/cred-form-dropdown-with-custom-posts/

I have created a view (items-for-dropdown) with the following output:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=1]{"value":"[wpv-post-id]","label":"[wpv-post-title]"}[wpv-item index=other],{"value":"[wpv-post-id]","label":"[wpv-post-title]"}</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>[/wpv-no-items-found][wpv-layout-end]

In that ticket it says to make sure the output is in JSON format. Not sure if this is correct? If not, how do I output in JSON?

I have then added the following to functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
    
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '374' ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

And this is my CRED form:

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']

    <div class="form-group">
		<label>item Parent</label>
		[cred_field field='_wpcf_belongs_item_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Amount</label>
		[cred_field field='amount' post='accessory' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Accessory Item</label>
		[cred_generic_field field="accessory-item" type="select" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[ [wpv-view name="items-for-dropdown"] ]
}
[/cred_generic_field]
	</div>

	[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]

But as you can see on the link below, nothing is displaying under the label.

Can you see what I've done wrong?

What is the link to your site? hidden link

#609282

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - I tried the code you shared with me and I can see the dropdown is displaying using your code on my local test site.

I would like to know are you using Access plugin - is it active?

#609323

Hi Minesh

I have Access installed and active, although I haven't set anything in it.

#609324

HAHAHA. So sorry.

I've just realised the theme was set to Divi, not the child theme I have added the code to.

Sorry for wasting your time. All seems to be working fine now.

#609326

Minesh
Supporter

Languages: English (English )

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

Ok - Could you please grant permission to 'guest' user to display the form.

More info:
https://toolset.com/documentation/user-guides/access-control-for-cred-forms/

#609333

Oh. I see the items are now appearing in the dropdown, but once I submit and go through to check on the backend, it does not seem to have saved the ID for the selected Item.

Should this field be a number? That's what I made it..

#609343

Minesh
Supporter

Languages: English (English )

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

You can do following. Actually, if you will see your database, postmeta table and try to find metakey "accessory-item", you will find that metakey with metavalue is saved.

In order to display it in backend, I would recommend you following thing.
1) Create a new number filed using Post field "accessory-item" under the post ground which is assigned to your desired post type.

2)
Go to CRED form - change "accessory-item" field name to "wpcf-accessory-item".

 [cred_generic_field field="wpcf-accessory-item" type="select" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[ [wpv-view name="items-for-dropdown"] ]
}
[/cred_generic_field]

3)
Try to create a new entry and check that field is populated with desired ID you selected while you create the new entry.

#609375

Hi Minesh

I did not understand what you meant in 1) above.

However, by changing "accessory-item" field name to "wpcf-accessory-item" on the CRED form, it seems to have fixed it.

Thanks for your help.