Assign foreach output to a php variable?
-
PLEASE HELP I’m new to PHP – this may be a simple issue but I have spent hours on it and I’m going mad.
I am trying to retrieve a list of custom posts where the author has a certain meta value.
I have no problems getting the custom posts.
I have no problems getting the authors with the specific author meta value.How to combine to two???
In my template file for the relevant page I have this code::
$authors = $wpdb->get_results("SELECT user_id FROM $wpdb->usermeta WHERE meta_value='Waterfront City'"); foreach ($authors as $author) { echo = $author->user_id.","; } $authorsids = "5,6,"; $args = array( 'post_type' => 'deal', 'author' => $authorsids, 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 6 ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();This does exactly what I want. Now for the BIG PROBLEM… $authorsids is not supposed to be defined manually at “5,6,” it is supposed to be defined authomatically using the results from the query on the Authors.
Now the output of the foreach is identical to $authorsids however I cannot assign the output of the foreach to a variable as it breaks.
What I’m trying to achieve in my mind is like this:
$authorsids = foreach ($authors as $author) { echo = $author->user_id.","; }now this obviously is incorrect so please tell me how to do this.
Thanks,
Nicole
The topic ‘Assign foreach output to a php variable?’ is closed to new replies.