Including variables in a custom select statement in WordPress

My custom select statement:

$querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->comments
    WHERE $wpdb->posts.ID = $wpdb->comments.comment_post_ID
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'post'
    GROUP BY $wpdb->posts.ID
    ORDER BY comment_count DESC
";

My aim is to limit the results returned to a particular set of post authors.

I have an array of WordPress post author user IDs in a variable $user_ids.

I thought about adding the following to my select statement, but I'm sure the following syntax isn't the right way to do it:

AND $wpdb->posts.post_author = $user_ids

Hoping someone can help show me the right way to do this?

Solutions

if $user_ids are coma separated values then you can do this

  AND $wpdb->posts.post_author in ( $user_ids)

if $user_ids is an array then use this :

   AND $wpdb->posts.post_author in ( implode( ',', $user_ids ))

First you should make a string:

$user_ids_string = implode(',',$user_ids)

then your syntax will be:

AND $wpdb->posts.post_author in ($user_ids_string)

Note: If your ids are not integers you should also wrap them with quotes ''

Tags: PHP / Mysql / Wordpress

Similar questions

Passing variables to templates (alternatives to globalizing variables)
I want to make my WP code simpler (there's some mess within header.php, page.php etc.). So, for example, I want to set something like this global: So $foo will work properly used anywhere in page.php, header.php, footer.php, loop.php and also within different theme files linked to functions.php (require_once). I already know two ways of achieving t...
Use global variables or function that returns said variables for site-wide private-ish WP settings?
I have a lot of functions extending various plugins and providing logic for a site, functions which use different bits of info that I would like to keep in one place and access it, but feel uncomfortable with using a global variable, out of fear that due to a vulnerability in my code they might get copied, or worse, overwritten in the global scope ...
Set variables for PHP files, so I could include these variables everywhere
I have SVG icons in PHP files, for example google-plus.php: I need to create variable $icon_google_plus associated with this file to include it and display the SVG code easily everywhere in my template (I'm using WordPress). If you can, I would like examples, how to include that variable in these situations (TheVariableHere = included $icon_google_...
Passing Form Variables Between If Statement
I'm working on a front end submission page for Wordpress that works in a number of steps. So I've got a form on a page and it's gathering data over a few stages and posting it after each stage. I can do this fine using GET, but I don't want the variables viewable in the URL because it would be easy for people to edit other posts on the blog which t...
Wordpress PHP: Conditional Statement wihtin a Conditional Statement
How would I place the following: Within the echo of this: Obviously I can't do this:
Fatal error: Namespace declaration statement has to be the very first statement or after any declare call, wordpress
My wordpress website shows the fatal error this how can I fix it? Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in /home/truevisio/public_html/wp-includes/sodium_compat/lib/sodium_compat.php on line 2

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.