Skip Navigation

[Resolved] Populate 'Select' generic field with post titles from two different post types

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
- 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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by Luo Yang 6 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#743217
Ticket_042618_noList.PNG
Ticket_042618_wList.PNG

I've just spent the last two days researching how to get a view of parent post titles to populate a generic field in a custom post child. I mostly used this ticket: https://toolset.com/forums/topic/populate-select-list-with-a-view/

SCENERIO: I have two custom posts, where one or the other can be the parent to a custom post child. Exec and Srmgr post titles are listed in a view (parents). The child post is 'Requests'. In the Request post I inserted a generic 'select' field.

PROBLEM: The view produces the list, but the generic field doesn't show on the front-end.
VIEW:
// Sanitize for viewing 'manager list' in select list field 'Requests'
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
function prefix_clean_view_output( $out, $id ) {
if ( $id == '744' ) { //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;
}
REQUEST CUSTOM POST:
<div class="row">
<label class="col-xs-2 col-sm-2 col-md-2 col-6 col-form-label form-group">Test:</label>
<div class="col-xs-6 col-sm-6 col-md-6 col-6 form-group">
<!--[wpv-view class='form-control' name="manager-list" order="asc"] -->
[cred_generic_field field='manager-list' type='select' class='form-control' urlparam='']
***WHAT SHOULD GO IN ‘FIELD=’ shortcode? ***
{
"required":0,
"validate_format":0,
"default":[],
"options":[wpv-view name='manager-list']
}
[/cred_generic_field]
</div>
</div>

Code in CHILD FUNCTIONS.PHP:
// Sanitize for viewing 'manager list' in select list field 'Requests'
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
function prefix_clean_view_output( $out, $id ) {
if ( $id == '744' ) { //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;
}

#745846
Ticket_042618_SuccessfulDropdown.PNG

I finally got the drop-down list of all names and it accepts a selection. SEE BELOW FOR CODE
QUESTIONS:
1). Do you notice anything in the code I should change. Is this the best way?
2). Now, what do I do with the Name (post title) selected? I want to set one of the parent fields with it's value (see attached image -- 'Test is the field I successfully created. The field right above the 'Test' field -- this is the parent field for exec. (I want to hide both exec and srmgr parent fields and then set one or the other -- exec or srmgr parent field with the value of 'Test' . Please point me in the right direction...
3). Any other words of wisdom, in some ways I feel so lost?

Successful drop-down list of names through implementing the following:
REQUEST FORM:
<div class="col-xs-6 col-sm-6 col-md-6 col-6 form-group">
[wpv-view class='form-control' name="manager-list" order="asc"]
</div>
VIEW:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<select id="FormControlSelect1" class="form-control wpv-loop js-wpv-loop">
<!-- class="wpv-loop js-wpv-loop"> -->
<wpv-loop>
<option>[wpv-post-body view_template="Loop item in Manager List"]</option>
</wpv-loop>
<!-- -->
</select>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

[wpv-post-title]

#746320

Q1) If it works fine in your website, you do not need to change anything

Q2) Please elaborate the question with more details:
I want to set one of the parent fields with it's value
Which one is the "parent field"? "Test" or "Submitted To"?
What value do you want to set? Where is the value come from?

That will help us to locate the problem. thanks

#747940

Thank you for your timely response.

REQUEST FORM: When the form was created it was populated with all the desired fields, including parent fields. I am assuming from things I’ve read through your guides and blog, I can hide some parent fields and set them dynamically through other user input (usually at the time of submitting the form). I also understand and am practicing using some parent fields displayed in the form, but maybe with Labels that make better sense to the user.

QUESTION 1: Are both of these true?

SCENARIO: A ‘Request’ is for work to be done (it is the start of creating an inventory of the work and finished product). The user must assign a manager to the work. A manager can be either an executive or a senior manager. The relationship is one manager to many requests, so the Request can be a child of either, but not both.

Rather than use the ‘belongs to parent…exec…or srmgr’ input fields in the form. I created a view to list all executive and senior manager names. The View is used as the input field for the manager. The ‘belongs to parent’ fields are hidden. I need to set one or the other hidden fields based on the value submitted to the Select field in the View.
*********************************************************************************************
EXECUTIVE – Possible parent to Request. Currently displayed as the "Submitted To" field to which referred.
[cred_field field='_wpcf_belongs_exec_id' value=' ' class='form-control' output='bootstrap']

SR. MANAGER– Possible parent to Request.
[cred_field field='_wpcf_belongs_srmgr_id ' value=' ' class='form-control' output='bootstrap']

VIEW (Value sets one of the Parent fields)
[wpv-view class='form-control' name="manager-list" order="asc" value='']

QUESTION 2). Please point me to the best pages for getting me started. How to do this in this environment (JS or php (“don’t have to write own php…”)). I think your Guide is well organized and robust, yet it seems a bit out of date and yet nothing is dated. I would like to write this, and if I have problems I’ll issue another ticket.

#764486

There are lots of questions in this thread, I am trying to answer them one by one.

Q1) I can hide some parent fields and set them dynamically through other user input

Here is the document about "Conditional Display for Form Inputs"
https://toolset.com/documentation/user-guides/conditional-display-for-form-inputs/
CRED lets you set conditional display for sections in forms. This means that some sections can display or hide, based on the values of custom fields and taxonomy inputs.

Q2) I need to set one or the other hidden fields based on the value submitted to the Select field in the View.

Your question is not clear. I assume we are talking about this, after user submit the CRED form for creating child post, you are going to add/edit the custom field of parent post.

If it is, it needs custom codes, I suggest you try with some PHP codes, when user submits the CRED form, use CRED action hook "cred_save_data" to trigger a PHP function, in this function get the parent post ID, and update it's custom field value.

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
This hook allows doing a custom action when post data is saved to database.

For example, this thread:
https://toolset.com/forums/topic/pass-on-a-value-from-a-parent-post-to-a-child-post-in-cred/#post-546635

Q3) Please point me to the best pages for getting me started.
I suggest you start with CRED document list:
https://toolset.com/documentation/user-guides/#cred-topics