Home › Toolset Professional Support › [Resolved] get fiels array from custom post type
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
Tagged: Types plugin
This topic contains 16 replies, has 3 voices.
Last updated by Jim 6 years ago.
Assisted by: Christian Cox.
Hello,
can you help me get the value's of two fields from a custom post type?
This is what I want to achieve:
function wppbc_get_us_states() {
$states = array(
// function here to get something similar as this:
//'AMS'=>'Amsterdam Hospital',
//'RTD'=>'Rotterdam Hospotal'
);
Both these values come from a custom post type. The title is the full name and the abbreviation is another field from this post type.
I hope you understand what I mean.
Can you show me what function (in the theme functions file)I should use to get this?
Thanks,
Jim
Hi Jim,
Thank you for contacting our support forum.
I'm not sure what u mean, can you let me know a bit more details?
Thanks,
Shane
Sorry, yes:
I need a function to retreive two values from a custom post type:
1. the title
2. another value from a specific field belonging to that post
I need it to be in this format:
'AMS'=>'Amsterdam Hospital',
'RTD'=>'Rotterdam Hospotal'
Because it must then be processed by another function that needs it in this format.
The purpose of this all is to populate a select form element with the data from this post type.
I got this example:
hidden link
In that example they hard coded the values, but I need it to come from my custom posty type.
More clear now?
Hi Jim,
Are the "AMS" and "RTD" formats stored on the posts already?
Please let me know.
Thanks,
Shane
the ams and rtd are example value's
I just need to get two value's from a custom post type; the title and another value from an extra field from that post type.
Have a look at the page I send you:
hidden link
I need the US states to be replaced by my post type.
Better?
Hi Jim,
Ok so you essentially want to create a select field with all the US states without typing it in.
If this code exists, why would you like to retrieve this same information from your posts.
I just want to ensure that i'm making the best suggestion that I can.
Looking forward to hearing from you.
Thanks,
Shane
Becasuse the client wants the ability to add more, and to be able to make adjustments to the names
Hi Jim,
You should be able to do it by using this function below.
To Get this to work you will need to replace the Text “Team Leader” with the name of the select field you wish to populate. Everything else is a Basic wordpress Query
add_filter( 'wpt_field_options', 'populate_select', 10, 3); function populate_select( $options, $title, $type ){ switch( $title ){ case 'Team leader': $options = array(); $args = array( 'post_type' => 'team-leader', 'post_status' => 'publish'); $posts_array = get_posts( $args ); foreach ($posts_array as $post) { $value = get_post_meta($post->ID,'wpcf-custom-field-slug'); $title = get_the_title($post->ID); $options[] = array( '#value' => $value[0], '#title' => $title ); } break; } return $options; }
So what you need to replace is the post_type "team-leader" with the slug of your CPT, "Team Leader" with the name of the select field that you want to be populated and 'wpcf-custom-field-slug' with the slug of the custom field that you want to get the value from.
Please let me know if this helps.
Thanks,
Shane
Hi Shane,
thanks for your help.
Since my select field is not from toolset I have to modify it to work with my form select field (from Profile Builder Pro).
I looked at the functions that work with my type of select field from that page (see link in my previous post) and changed it for as far as I understand with your function.
This came out (but does not work...) Do you see what's wrong?
add_filter( 'wppb_select_options_array', 'wppbc_test_ziekenhuizen_select_options', 20, 5); function wppbc_test_ziekenhuizen_select_options( $options, $field, $form_location, $user_id, $request_data) { switch( $field ){ case 'test_ziekenhuizen': $options = array(); $args = array( 'post_type' => 'ziekenhuis', 'post_status' => 'publish'); $posts_array = get_posts( $args ); foreach ($posts_array as $post) { $value = get_post_meta($post->ID,'ziekenhuis-id'); //$title = get_the_title($post->ID); $options[] = array( '#value' => $value[0] //'#title' => $title ); } break; } return $options; } add_filter( 'wppb_select_labels_array', 'wppbc_test_ziekenhuizen_select_labels', 20, 5); function wppbc_test_ziekenhuizen_select_labels( $labels, $field, $form_location, $user_id, $request_data) { switch( $field ){ case 'test_ziekenhuizen': $labels = array(); $args = array( 'post_type' => 'ziekenhuis', 'post_status' => 'publish'); $posts_array = get_posts( $args ); foreach ($posts_array as $post) { //$value = get_post_meta($post->ID,'ziekenhuis-id'); $title = get_the_title($post->ID); $labels[] = array( //'#value' => $value[0], '#title' => $title ); } break; } return $labels; }
Hi Jim,
From what I understand from the document.
The function that gets the states is this one.
function wppbc_get_us_states() { $states = array( 'AL'=>'Alabama', 'AK'=>'Alaska', 'AZ'=>'Arizona', 'AR'=>'Arkansas', 'CA'=>'California', 'CO'=>'Colorado', 'CT'=>'Connecticut', 'DE'=>'Delaware', 'DC'=>'District of Columbia', 'FL'=>'Florida', 'GA'=>'Georgia', 'HI'=>'Hawaii', 'ID'=>'Idaho', 'IL'=>'Illinois', 'IN'=>'Indiana', 'IA'=>'Iowa', 'KS'=>'Kansas', 'KY'=>'Kentucky', 'LA'=>'Louisiana', 'ME'=>'Maine', 'MD'=>'Maryland', 'MA'=>'Massachusetts', 'MI'=>'Michigan', 'MN'=>'Minnesota', 'MS'=>'Mississippi', 'MO'=>'Missouri', 'MT'=>'Montana', 'NE'=>'Nebraska', 'NV'=>'Nevada', 'NH'=>'New Hampshire', 'NJ'=>'New Jersey', 'NM'=>'New Mexico', 'NY'=>'New York', 'NC'=>'North Carolina', 'ND'=>'North Dakota', 'OH'=>'Ohio', 'OK'=>'Oklahoma', 'OR'=>'Oregon', 'PA'=>'Pennsylvania', 'RI'=>'Rhode Island', 'SC'=>'South Carolina', 'SD'=>'South Dakota', 'TN'=>'Tennessee', 'TX'=>'Texas', 'UT'=>'Utah', 'VT'=>'Vermont', 'VA'=>'Virginia', 'WA'=>'Washington', 'WV'=>'West Virginia', 'WI'=>'Wisconsin', 'WY'=>'Wyoming', ); return $states; }
So if we modify this to build the dynamic array then it should work.
function wppbc_get_us_states() { $options = array(); $args = array( 'post_type' => 'team-leader', 'post_status' => 'publish'); $posts_array = get_posts( $args ); foreach ($posts_array as $post) { $value = get_post_meta($post->ID,'wpcf-custom-field-slug'); $title = get_the_title($post->ID); $options[] = array( '#value' => $value[0], '#title' => $title ); } return $options; }
Please try this and let me know the results.
It also seems that these functions are needed as well.
add_filter('wppb_select_options_array', 'wppbc_us_states_select_options', 20, 5); function wppbc_us_states_select_options($options, $field, $form_location, $user_id, $request_data) { if ($field['meta-name'] != 'us_states') return $options; $states = array_keys(wppbc_get_us_states()); return $states; } add_filter('wppb_select_labels_array', 'wppbc_us_states_select_labels', 20, 5); function wppbc_us_states_select_labels($labels, $field, $form_location, $user_id, $request_data) { if ($field['meta-name'] != 'us_states') return $labels; $states = array_values(wppbc_get_us_states()); return $states; } add_filter('wppb_extra_select_option', 'wppbc_us_states_dummy_option', 20, 3); function wppbc_us_states_dummy_option($return, $field, $item_title) { if ($field['meta-name'] != 'us_states') return $return; return '<option value="" selected>Select an option</option>'; }
Thanks,
Shane
Hi Shane,
yes now we are getting somewhere, you took a look at the page.
You said: "It also seems that these functions are needed as well."
That's exactly what I was trying to change your first function into.
The first function (wppb_select_options_array) gets the array keys (=options) and the second function (wppb_select_labels_array) gets the array value's (=labels)
The "wppbc_get_us_states" should therefore generate an array with keys and values but at the moment it generates a different format I think?
Hi Jim,
So as you say we are getting close but the array is generated differently?
Could you try this.
foreach ($posts_array as $post) { $value = get_post_meta($post->ID,'wpcf-custom-field-slug'); $title = get_the_title($post->ID); $options[] = array( $value[0] => $title, ); }
Let me know if when you replace the foreach with the one above if the correct list is generated.
Thanks,
Shane
Hi Shane,
The getting close thing was because you actually took a look at the page/document 😉
I am afraid the code still does not work...
This is the result:
<select name="test_ziekenhuizen" id="test_ziekenhuizen" class="custom_field_select"> <option value="" class="custom_field_select_option " disabled="" selected="">Test Ziekenhuizen</option> <option value="0" class="custom_field_select_option "></option> <option value="1" class="custom_field_select_option "></option><option value="2" class="custom_field_select_option "></option> </select>
It is correct that there are only 3 posts.
But the values are not correctly loaded into the other function.
Any other idea?
Hi Jim,
Could you allow me to have admin access to the area where you are testing this code?
I would like to do some debugging to ensure that the information is being printed out
Thanks,
Shane
Hi Shane,
I got a reply from the other developer. He helped me get the array right.
This is the correct function now:
You need to fill up the array like this:
function wppbc_get_ziekenhuizen() { $options = array(); $args = array( 'post_type' => 'ziekenhuis', 'post_status' => 'publish' ); $posts_array = get_posts( $args ); foreach ($posts_array as $post) { $value = get_post_meta($post->ID,'wpcf-ziekenhuis-id'); $title = get_the_title($post->ID); $options[ $value[0] ] = $title; } return $options; }
Glad we got this working again ?