Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this one with <code>WP_Query</code> no need of relation just use the compare part with <code>IN</code></p> <pre><code> $user_ids = array(60, 61, 62, 63); $args = array( 'post_type' =&gt; 'post', 'meta_key' =&gt; 'likes', 'post_status' =&gt; 'publish', 'posts_per_page' =&gt; -1, 'orderby' =&gt; 'rand', 'order' =&gt; 'ASC', 'meta_query' =&gt; array( array( 'key' =&gt; 'likes', 'value' =&gt; $user_ids, //array 'compare' =&gt; 'IN', ) ) ); $query = new WP_Query($args); </code></pre> <blockquote> <p>See the given example above "Time Parameters" Heading <a href="http://codex.wordpress.org/Class_Reference/WP_Query" rel="noreferrer"><strong>WP_Query</strong></a></p> </blockquote> <p>OR by get_posts try this </p> <pre><code>$args = array( 'post_type' =&gt; 'post', 'post_status' =&gt; 'publish', 'posts_per_page' =&gt; -1, 'orderby' =&gt; 'rand', 'meta_query' =&gt; array( array( 'key' =&gt; 'likes', 'value' =&gt; $user_ids, 'compare' =&gt; 'IN' ) ) ); $posts = get_posts($args); </code></pre> <p>OR by a custom query you can do as follows</p> <pre><code>global $wpdb; $liked_posts=$wpdb-&gt;get_results("SELECT * FROM `wp_posts` WHERE post_type='post' AND post_status ='publish' AND ID IN( SELECT post_id FROM `wp_postmeta` WHERE meta_key='likes' AND meta_value IN (".join(',',$user_ids).") ) ORDER BY RAND()"); </code></pre> <p>Also don't store array of ids in one row instead loop through ids array and <strong>normalize</strong> your likes data manually Don't serialize data into a database field. That's what <a href="http://en.wikipedia.org/wiki/Database_normalization" rel="noreferrer"><strong>Database_normalization</strong></a> is for and insert each id in new row because </p> <blockquote> <p>Wordpress stores the meta values as strings. When you pass <code>update_post_meta</code> an array, it automatically converts it to a string. What you'll need to do is <code>unserialize</code> it when you attempt to read the data but in mysql you can't <code>unserialize</code> column data in the query because mysql doesn't about the serialization of php </p> </blockquote> <pre><code>$likes = array(61, 62); foerach($likes as $l){ update_post_meta($post-&gt;ID, "likes", $l); } </code></pre> <p>And below is the query you have asked for in bounty comments </p> <blockquote> <p><strong><em>If a $wpdb answer is given, then please elaborate on how to factor in category (include and exclude), and ignore posts by an array of IDs</em></strong></p> </blockquote> <pre><code>$liked_posts=$wpdb-&gt;get_results(" SELECT * FROM `wp_posts` wp INNER JOIN `wp_term_relationships` wtr ON (wp.`ID`=wtr.`object_id`) INNER JOIN `wp_term_taxonomy` wtt ON (wtr.`term_taxonomy_id` =wtt.`term_taxonomy_id`) WHERE wp.post_type='post' AND wp.post_status ='publish' AND wp.ID IN( SELECT post_id FROM `wp_postmeta` WHERE meta_key='likes' AND meta_value IN (".join(',',$user_ids).") ) AND wp.ID NOT IN (100,101,102) AND wtt.`term_id` IN(1,2,3) AND wtt.`term_id` NOT IN (4,5,6,) ORDER BY RAND() "); </code></pre> <p>I have used the <code>INNER JOINs</code> on <code>wp_term_relationships</code> and <code>wp_term_taxonomy</code> the table <code>wp_term_relationships</code>stores the relation of posts and the taxonomy of category and the table <code>wp_term_taxonomy</code> have the taxonomy of the category and also the category id </p> <p>Here is the part which covers the </p> <p><strong>1. category (include and exclude)</strong></p> <pre><code>AND wtt.`term_id` IN(1,2,3) AND wtt.`term_id` NOT IN (4,5,6,) </code></pre> <p><strong>2. ignore posts by an array of IDs</strong></p> <pre><code>AND wp.ID NOT IN (100,101,102) or $postids =array(100,101,102); AND wp.ID NOT IN (".join(',',$postids).") </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload