gpt4 book ai didi

php - WordPress 元查询数组

转载 作者:可可西里 更新时间:2023-11-01 07:45:48 24 4
gpt4 key购买 nike

我正在构建一种让我的用户“喜欢”帖子的方式。

我会有一组喜欢帖子的用户:

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

然后帖子将有一个 post_meta,当在帖子上单击“喜欢”按钮时,它会保存用户 ID,如下所示:

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

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

那么如何返回用户喜欢的所有帖子。这就是我目前的做法,但行不通。

$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 个结果。

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

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

谢谢!

最佳答案

WP_Query 试试这个,不需要关系,只需使用 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);

See the given example above "Time Parameters" Heading WP_Query

或者通过 get_posts 试试这个

$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);

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

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 插入新行,因为

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);

}

下面是您在赏金评论中要求的查询

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

$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 JOINswp_term_relationships 存储帖子和类别的分类法和表 wp_term_taxonomy 有类别的分类法和类别 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).")

关于php - WordPress 元查询数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18036471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com