Support » Themes and Templates » 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.


Viewing 2 replies - 1 through 2 (of 2 total)
  • One more thing I forgot to add in my post above is that I have to do it this way as I am using pagination.

    Without pagination I could achieve the desired output using an if statement relating to the specific author meta within the post loop.

    However this doesn’t work with pagination as the pagination is shown for all retrieved posts not just those displayed.

    Really no-one can help with this? I’m seriously stuck and I’m sure someone out there must know the answer.

    What I’m trying to do is really quite simple. The site Authors are either one of two types (as defined by a custom field in the sure profile) and I need a page which shows only posts by an author of type one and another page which show posts by an author of type 2.

    Can someone please help?

    Please see my first post as to how I tried to solve it. If you have a completely different suggestion I will try anything.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Assign foreach output to a php variable?’ is closed to new replies.