【发布时间】:2018-02-16 11:34:09
【问题描述】:
最终结果匹配一个 'post_id' 数组,按参加者人数排序。我知道这是一个 WP 后端,但我必须在 SQL 中完成,没有 'get_posts' 或任何与 WordPress 相关的东西。我想做的事情令人困惑,所以我会尽量清楚。
我必须开始:
- $check_post_ids - 我需要检查的原始 post_ids 数组
- $start_date - 'meta_value' 每个事件的 '_StartDate' 需要匹配。
我需要做什么:
我需要检查这三个 post_id 以查看它们是否具有匹配的开始日期。如果他们这样做了,我需要得到一个从参加者人数最多到最少的 post_id 数组。
目前我正计划使用多个 SELECT 和 foreach() 语句来执行此操作,但我觉得必须有一种更简单的方法来执行此操作(即一个 SELECT 和 foreach() )。这就是我现在正在做的事情。我还没有完成它,因为我觉得必须有一种更简单的方法来做到这一点。非常感谢 SQL 新手和任何帮助!
$check_post_ids = array('484', '627', '982', '2435');
$start_date = '1963-10-20 19:30:00';
// iterate through array of post_ids and check if they have the same _StartDate
foreach($check_post_ids as $id){
$start_date_check = "SELECT * FROM `wp_postmeta` WHERE `post_id` =" . $id . " AND `meta_key` LIKE '_StartDate' AND `meta_value` = '" . $start_date . "'";
$start_date_check_result = mysqli_query($conn, $start_date_check);
// assign all post_ids with a matching _StartTime to a new array
if (mysqli_num_rows($start_date_check_result) > 0) {
while($row = mysqli_fetch_assoc($start_date_check_result)) {
$matching_post_ids[] = $row['post_id'];
// iterate through matching_post_ids array, get the _NumAttendees for each, and assign their id and _NumAttendees to an assoc array to be sorted
foreach($matching_post_ids as $id){
$attendees_check = "SELECT meta_value FROM wp_postmeta WHERE post_id = " . $id . " AND meta_key = '_ecp_custom_2'";
$attendees_check_result = mysqli_query($conn, $attendees_check);
if($upvotes_check > 0){
while($row = mysqli_fetch_assoc($attendees_check_result)){
$sort_by_attendees['id'] = $id;
$sort_by_attendees['attendees'] = $row['meta_value'];
$sort_by_attendees_array[] = $sort_by_attendees;
}
}
}
【问题讨论】:
-
再次搞砸了,我可以看到我的代码有点不正常,但希望它可以帮助你了解基本的想法。
标签: php mysql sql wordpress mysqli