WordPress元查询数组 [英] WordPress Meta Query Arrays

查看:130
本文介绍了WordPress元查询数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一种使用户喜欢"帖子的方式.

I'm building a way form my users to "like" a post.

我将拥有一系列喜欢帖子的用户:

I'll have an array of users that have liked posts:

$user_ids = array(60, 61, 62, 63);

然后,帖子中将具有一个post_meta,当在帖子上单击"like"按钮时,该用户名将保存用户ID,如下所示:

And then a post will have a post_meta that saves the user ID when the "like" button is clicked on a post, like so:

// Build array of user IDs that have liked this post
$likes = array(61, 62);

// Save array
update_post_meta($post->ID, likes, $likes);

那么我该如何返回用户喜欢的所有帖子.这是我目前的操作方式,这是行不通的.

So how can I return all posts liked by the users. This is how I'm doing it currently, which doesn't work.

$args = array(
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'orderby'           => 'rand',
    'meta_query'        => array(
        'relation'  => 'AND',
        array(
            'key'       => 'likes',
            'value'     => $user_ids,
            'compare'   => 'IN'
        )
    )
);
$posts = get_posts($args);

这应该返回所有在帖子喜欢"元中保存有"60、61、62、63"的帖子.但它返回0个结果.

This should return all posts that have '60, 61, 62, 63' saved in the posts 'likes' meta. But it returns 0 results.

现在,我认为这是因为WordPress将数组保存在post_meta中时会对其进行序列化.

Now, I think this is because WordPress serializes arrays when they are saved in a post_meta.

那么,我该如何进行这样的查询?可能吗?也许我必须使用$ wpdb类.

So, how do I a do a query like this? Is it even possible? Maybe I have to use the $wpdb class.

谢谢!

推荐答案

尝试使用WP_Query进行此操作,无需关联,只需将比较部分与IN

Try this one with WP_Query no need of relation just use the compare part with IN

 $user_ids = array(60, 61, 62, 63);
 $args = array(
   'post_type' => 'post',
   'meta_key' => 'likes',
   'post_status'       => 'publish',
   'posts_per_page'    => -1,
   'orderby'           => 'rand',       
   'order' => 'ASC',
   'meta_query' => array(
       array(
           'key' => 'likes',
           'value' => $user_ids, //array
           'compare' => 'IN',
       )
   )
 );
 $query = new WP_Query($args);

请参见上面时间参数"标题下的给定示例 WP_Query

OR by get_posts试试这个

OR by get_posts try this

$args = array(
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'orderby'           => 'rand',
    'meta_query'        => array(

        array(
            'key'       => 'likes',
            'value'     => $user_ids,
            'compare'   => 'IN'
        )
    )
);
$posts = get_posts($args);

或者通过自定义查询,您可以执行以下操作

OR by a custom query you can do as follows

global $wpdb;

$liked_posts=$wpdb->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()");

也不要在一行中存储ID数组,而是循环遍历id数组并手动规格化自己喜欢的数据不要将数据序列化到数据库字段中.这就是 Database_normalization 的用途,并将每个ID插入新行,因为

Also don't store array of ids in one row instead loop through ids array and normalize your likes data manually Don't serialize data into a database field. That's what Database_normalization is for and insert each id in new row because

Wordpress将元值存储为字符串.当你通过时 update_post_meta一个数组,它将自动将其转换为字符串. 当您尝试阅读时,您需要做的是unserialize 数据,但在mysql中您不能在查询中unserialize列数据,因为mysql与php的序列化无关

Wordpress stores the meta values as strings. When you pass update_post_meta an array, it automatically converts it to a string. What you'll need to do is unserialize it when you attempt to read the data but in mysql you can't unserialize column data in the query because mysql doesn't about the serialization of php

$likes = array(61, 62);

foerach($likes as $l){

update_post_meta($post->ID, "likes", $l);

}

以下是您在悬赏评论中所要求的查询

And below is the query you have asked for in bounty comments

如果给出了$ wpdb的答案,请详细说明如何考虑类别(包括和排除),并按ID数组忽略帖子

$liked_posts=$wpdb->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() ");

我在wp_term_relationshipswp_term_taxonomy上使用了INNER JOINs,表wp_term_relationships存储了职位和类别分类的关系,表wp_term_taxonomy具有类别的分类以及类别ID

I have used the INNER JOINs on wp_term_relationships and wp_term_taxonomy the table wp_term_relationshipsstores the relation of posts and the taxonomy of category and the table wp_term_taxonomy have the taxonomy of the category and also the category id

这是涵盖

1.类别(包括和排除)

AND wtt.`term_id` IN(1,2,3) AND wtt.`term_id` NOT IN (4,5,6,) 

2.通过ID数组忽略帖子

AND wp.ID NOT IN (100,101,102) 
or $postids =array(100,101,102);
AND wp.ID NOT IN (".join(',',$postids).")

这篇关于WordPress元查询数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆