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
Related
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
my first query is okay
$ids = [];
$novidades = get_posts( array(
'posts_per_page' => 4,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
) );
if ( count( $novidades ) ) {
foreach( $novidades as $novidade ) {
$ids[] = $novidade->ID;
}
}
//rest of my code is ok
but, i try post another post and ignore the first query, but don't work, list all post
$args2 = array(
'post_type' => 'post',
'posts__not_in' => $ids
);
$featured = new WP_Query($args2);
Can help me?
It's post__not_in. Remove the extra s from your code.
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored.
Your code should be:
$args2 = array(
'post_type' => 'post',
'post__not_in' => $ids,//<====extra 's' removed
);
$featured = new WP_Query($args2);
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'm using Barcelona theme and I need to get authors who have posted in certain category.
In my author.php template I have:
$barcelona_authors = get_users( array(
'fields' => 'ID',
'who' => 'authors',
'order' => 'DESC',
'orderby'=> 'post_count'
) );
<?php
foreach ( $barcelona_authors as $barcelona_author_id ) {
barcelona_author_box( $barcelona_author_id, false );
}
?>
How to get authors who have posted to category ID 59?
For example I tried with:
$barcelona_authors = get_posts('category=59');
But I'm getting error. Any help?
ERROR:
Notice: Object of class WP_Post could not be converted to int in
/home/wp-includes/author-template.php on line 296
Your get_posts( 'category=59' ) code should work. I have tested the following on one of my test wordpress installation and it works (with a different category ID of course). If you get an error during the get_posts call you need to show us the error.
<?php
$category_posts = get_posts( 'category=59' );
$authors_ids = array();
$authors = array();
foreach( $category_posts as $cat_post ) {
$authors_ids[] = $cat_post->post_author;
}
// Not sure if you need more data than just the ID so here is what you need
// to get other fields.
foreach( $authors_ids as $id ) {
$user = get_user_data( $id );
// Display name: $user->display_name;
// Nice name: $user->user_nicename;
// etc...
}
?>
This was the solution for Barcelona theme:
$barcelona_posts = get_posts('cat=59');
$barcelona_author_ids = array();
foreach ( $barcelona_posts as $k => $v ) {
if ( ! in_array( $v->post_author, $barcelona_author_ids ) ) {
$barcelona_author_ids[] = $v->post_author;
}
}
$barcelona_authors = get_users( array(
'fields' => 'ID',
'who' => 'authors',
'order' => 'DESC',
'orderby'=> 'post_count',
'include' => $barcelona_author_ids
) );
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
}
}