1

I'm currently storing results in a variable from a db table like this :

$followed = $wpdb->get_results("SELECT user_id1 FROM wp_um_followers WHERE user_id2 = 1", ARRAY_A);

This returns an array like this :

Array ( [0] => Array ( [user_id1] => 144 ) [1] => Array ( [user_id1] => 50 ) [2] => Array ( [user_id1] => 42 ) [3] => Array ( [user_id1] => 829 ) [4] => Array ( [user_id1] => 826 ) [5] => Array ( [user_id1] => 822 ) [6] => Array ( [user_id1] => 823 ) [7] => Array ( [user_id1] => 821 ) [8] => Array ( [user_id1] => 820 ) [9] => Array ( [user_id1] => 819 ) [10] => Array ( [user_id1] => 818 ) [11] => Array ( [user_id1] => 816 ) [12] => Array ( [user_id1] => 817 ) [13] => Array ( [user_id1] => 814 ) [14] => Array ( [user_id1] => 815 ) [15] => Array ( [user_id1] => 15 ) [16] => Array ( [user_id1] => 93 ) [17] => Array ( [user_id1] => 844 ) ) 

Now I'd like to set up a new query using the user_id1 values from the above array as the values for a multiple author query on a custom post type.

Something like this :

$get_these_posts = array( 'post_type' => 'stream', 'post_status' => 'publish', 'posts_per_page' => '10', 'paged' => $paged, ( array( 'author__in' => array( $followed ) ) ) );

Which should return posts only from authors who appear in the original query stored in the $followed variable.

But this isn't working obviously as I'm not targeting the correct part of the array [user_id1] and I don't know how to.

I'm not even sure that the second query is set up right, or if it indeed can be set up in this way at all?

Any help will be greatly appreciated.

1

You need to pass author__in as an array but that isn't what you doing with this: ( array( 'author__in' => array( $followed ) ) ). You are creating an odd set of nested arrays. WP_Query isn't going to know what to do with it. A much simplified version should work:

$followed = $wpdb->get_col("
  SELECT user_id1 
  FROM wp_um_followers 
  WHERE user_id2 = 1"
);

$get_these_posts = array( 
  'post_type' => 'stream', 
  'post_status' => 'publish', 
  'posts_per_page' => '10', 
  'paged' => $paged, 
  'author__in' => $followed
);

Notice I used get_col(). That will return a simple array of IDs.

  • Thank you @s_ha_dum, that works perfectly. I knew I was doing it wrong, just didn't know how to set it up properly. One other question, do you know how I can define an extra author to add to the array? Can 'author' => be used alongside author__in => in the post query for example? – Grant Sep 3 '15 at 21:05
  • Ahh just tried it and it seems like yes you can. Excellent ;) – Grant Sep 3 '15 at 21:08
  • Just append the extra author I'd to the array you already have – s_ha_dum Sep 3 '15 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.