I have this code:
while( $animes->have_posts() ) {
$animes->the_post();
$i++;
$animeID[$i] = $post->ID;
$args = array(
'orderby' => 'meta_value_num',
'order' => 'DESC',
'fields' => 'all',
'meta_query' => [['key' => 'episode_number','type' => 'NUMERIC',]]
);
$episodes[$i] = wp_get_post_terms(intval( $animeID[$i] ), 'episodes', $args );
}
I want to merge all $episodes[$i] into one array. Is this possible?
You have instantiated $episodes outside of your loop right?
If I understand you correctly, you have an array of arrays now in $episodes and you want it to be one array of values instead? So kind of like flattening the array? It might help if you explain why you want to do this. But perhaps something like this:
$episodes = [];
while( $animes->have_posts() ) {
$animes->the_post();
$args = array(
'orderby' => 'meta_value_num',
'order' => 'DESC',
'fields' => 'all',
'meta_query' => [['key' => 'episode_number','type' => 'NUMERIC',]]
);
$episodes[$i] = wp_get_post_terms(intval( $post->ID ), 'episodes', $args );
}
function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}
$flattened_episodes = flatten($episodes);
var_dump($flattened_episodes);
See How to Flatten a Multidimensional Array? for more on flattening an array. Docs here on the array_walk_recursive function - https://php.net/array_walk_recursive
Related
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
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.
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
I have an array of Wordpress page parent IDs which I then use a FOREACH loop to replace each parent ID with the name of the page (parent).
The problem is the array is sorted by the ID order, and I Want to now re-order it using the titles.
How can I do this?
function bv_quick_select_get_category_options() {
$procedure_pages = get_posts(array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-procedure-minimal.php'
)
)
));
$procedure_categories = array();
foreach($procedure_pages as $p1) {
array_push($procedure_categories, wp_get_post_parent_id($p1->ID));
}
$output = '';
foreach(array_unique($procedure_categories) as $c) {
$output .= '<option value="'.get_post($c)->post_title.'">'.get_post($c)->post_title.'</option>';
}
return $output;
}
I would think it would be more efficient to fetch the categories with one query based on the ids, and do the sorting by title in the database. This is my best attempt at how to do that from a quick read of the wordpress docs, but I'm not a wordpress user so it probably needs some work.
function bv_quick_select_get_category_options() {
$procedure_pages = get_posts(array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-procedure-minimal.php'
)
)
));
$ids = array_column($procedure_pages, 'ID');
$categories = get_posts([
'include' => $ids,
'order_by' => 'title'
]);
$output = '';
foreach($categories as $c) {
$output .= '<option value="'.$c->post_title.'">'.$c->post_title.'</option>';
}
return $output;
}
I'm clearly not doing this right. Any help would be greatly appreciated. Essentially i am pulling nearby zip codes and city names to a search that is being done. i then want to use WP_Query to pull the WordPress post types matching those zip codes and city names. I'm not super familiar with WordPress, so it is highly likely i'm way off base on how to do this:
$url = "http://www.zipcodeapi.com/rest/OeAp3k78myEhBy0oqSlQSlUWOt6N7TjW8Tlbdtkz1YRCwS1WKmNDIHzwbFjizCeI/radius.json/" . $searchbox . "/100/km";
$response = file_get_contents($url);
$json = json_decode($response);
$post_type = 'location';
$citysearcharray = array();
$zipsearcharray = array();
$searcharray = array();
foreach($json->zip_codes as $nearbyzip)
{
$citysearcharray[] = array(
'key' => 'city',
'value' => $nearbyzip->city,
'compare' => '='
);
$zipsearcharray[] = array(
'key' => 'zip_code',
'value' => $nearbyzip->zip_code,
'compare' => '='
);
}
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'caller_get_posts'=> 1,
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'OR',
$citysearcharray,
$zipsearcharray));
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$count=0;
echo '<ul class="location">';
while ($my_query->have_posts()) : $my_query->the_post();
...
Amy i putting the $args together correctly?
TIA
In your foreach, instead of adding new full values to your arrays
$zipsearcharray
and
$citysearcharray
have the value key as an array so:
'value' => array(array of cities)
then change
compare => 'IN'
I am needing some assistance. My goal is to query "clinics" post type by array of zip codes. Then get the ID's of those clinics and run another query of post type called "clinicspromo" to get those results. Then inside the loop, run query #3 to retrieve the clinic information again that is tied to each clinic promo. I have most of it completed, I am just having a problem turning the results of $post->ID; to an array of ID's separated by commas just like the zip code list. Any help would be appreciated!
$zipcodelist = '90001, 90002, 90003';
$args = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinics',
'meta_query' => array (
array (
'key' => 'cliniczipcode',
'value' => $zipcodelist,
'compare' => 'IN'
)
) );
$postlist = get_posts( $args );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$argstwo = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinicpromos',
'meta_query' => array (
array (
'meta_key' => 'assignclinic',
'meta_value' => $current,
'compare' => 'IN'
)
) );
$the_query = new WP_Query( $argstwo );
Changed your foreach loop like: I think you have an extra + sign when storing the IDs.
This is your loop:
foreach ( $postlist as $post ){
$posts[] += $post->ID;
}
Replace it with:
foreach ( $postlist as $post ){
$posts[] = $post->ID;
}
Try this, you do not need +
foreach ( $postlist as $post ) {
$posts[] = $post->ID;
}
I have been struggling for a couple days with trying to use the data in a custom field to return posts. Here is my function that I have in functions.php. I am able to return my posts, except that they aren't limited to the ones defined in the $json variable. I can decode the json and return the array...but I can't seem to convert it in such a way that it fills in my array properly for the "posts__in" in the $dishes_arg.
Can anyone help me identify what I am doing wrong?
add_action ('woo_loop_before', 'hgf_home_menu');
function hgf_home_menu () {
if (is_home() || is_front_page()) {
wp_reset_query();
global $posts;
$menu_args = array(
'post_type'=>'single_menu',
'posts_per_page' => 1,
'meta_key' => 'orderby_date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'orderby_date', // Order-By Date field is upcoming
'value' => date("Y-m-d"),
'compare' => '>='
),
array(
'key' => 'orderby_date', // Order-By Date isn't more than two weeks out
'value' => date("Y-m-d", strtotime( "+2 weeks")),
'compare' => '<='
)
),
);
// Get menu that meets criteria
$menus = new WP_Query( $menu_args );
// Show menu that meets criteria
if ( $menus->have_posts() ) {
while ( $menus->have_posts() ) {
$menus->the_post();
}
wp_reset_postdata();
// Get the menu's product/post listing
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$array = json_decode($json);
$include = array();
foreach($array as $a) {
$include[] = $a->ID;
}
$args = array(
'posts_per_page' => -1,
'include' => $include);
$posts = get_posts($args);
$dish_args = array(
'post_type' => 'product',
'post__in' => $posts,
);
// Get dishes in menu listing
$dishes = get_posts( $dish_args );
if ($dishes) {
foreach ($dishes as $dish) {
}
}
} else { // no posts found }
/* Restore original Post Data */
wp_reset_postdata();
}
}
You jSon property is id not ID
$include[] = $a->ID;
Should be
$include[] = $a->ID;
Where/why is there jSon here? Is this coming from something else.
Otherwise that could just be a simple delimitated list or even a normal PHP array.
I figured it out...turns out I just had to cut out a bunch of code that was getting in the way: $include = array() ... $args = array() ... $dish_args = array() ... $dishes = get_posts(). Here is the corrected portion:
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$dishes = json_decode($json);
if ($dishes) {
foreach ($dishes as $dish) {
// Echo titles and permalinks
}
}