I would like to know the simplest way to add the value of the $titles->post_title to become the key and value of an array.
Here is my code:
$data_from_database = array();
$titles = get_posts( array(
'post_type' => 'resort',
'order' => 'ASC'
) );
foreach($data_from_database as $field_key => $field_value) {
$field['choices'][$field_key] = $field_value;
$field['choices'][$field_value] = $field_value;
}
Desired result:
$data_from_database = array('1value' => '1value', '2value' => '2value',
'3value' => '3value');
I have looked and read other posts about this but wasnt able to find any info to achieved what i want to do.
Thanks for your answers in advance
Thanks for the answers guys..
I figured it out by using this code.
$data_from_database = array();
$myarray = array();
$titles = get_posts( array( 'post_type' => 'resort') );
$new_title = wp_list_pluck($titles, 'post_title', 'post_title');
// reset choices
$field['choices'] = array();
// if has rows
foreach($new_title as $field_key => $field_value) {
$field['choices'][$field_key] = $field_value;
}
// return the field
return $field;
wordpress has a built in function to automatically push values and keys to an array
Your question is completely unclear so try to add more details to get more complete answers.However based on you desired output
$data_from_database = array('1value' => '1value', '2value' => '2value', '3value' => '3value');
and this:
I would like to know the simplest way to add the value of the $titles->post_title to become the key and value of an array.
you can alter your code to look like this:
$data_from_database = array();
$titles = get_posts( array(
'post_type' => 'resort',
'order' => 'ASC'
) );
foreach($titles as $field_key => $field_value) {
$data_from_database[$field_key] = $field_key;
}
Try this code to get your desired output
$data_from_database = array();
$titles = get_posts( array(
'post_type' => 'news',
'order' => 'ASC'
) );
foreach($titles as $value) {
$data_from_database[$value->post_title] = $value->post_title;
}
Hope this helps you.