postmeta table example走走meta_value'匹配“POST_ID”和“meta_key”,“meta_value”二合一
最终的结果是匹配的“POST_ID”阵列,由出席者的数量来分类的搜索时。我知道这是一个WP后端,但我必须在SQL中完成,没有'get_posts'或任何与WordPress相关的东西。我想要做的是混乱,所以我会努力澄清。
我有什么开始:
- $ check_post_ids - post_ids我需要检查的原始数组
- $ START_DATE - 在'meta_value'每个事件的“ _StartDate'需要匹配。
我需要做什么:
我需要检查这三个post_ids,看看他们是否有匹配的开始日期。如果他们这样做,我需要获得从最高到最低数量的与会者排序的post_ids数组。
目前我打算多的SELECT和foreach()语句这样做,但我觉得必须有这样做(即一个选择&的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;
}
}
}
再次弄砸它,我可以看到我的代码是各种各样的重击,但希望它可以帮助你获得基本的想法。 – AndrewTelkamp