挑战:
Get n=从最近发布的具有相同类别和/或标签的帖子中获取较旧和较新的帖子数量。
解决方案:
就像@TwerkingN00b建议的那样,我们确实需要一些自定义SQL。我们正在寻找的神奇SQL短语是
UNION.
UNION用于将多个SELECT语句的结果合并到单个结果集中。
所有其他的东西都是WordPress如何处理SQL查询的知识,如何获取相关数据,然后将其组合在一起。让我们开始:
设置基础知识。我们的参考帖子(=当前ID,如果不支持),其日期和一些空变量。此外,我们希望引导查询忽略或接受相关类别或术语。
function the_assoc_posts($post_id = 0, $quantity = 3, $ignore_categories = false, $ignore_tags = false) {
if (empty($post_id)) $post_id = get_the_ID();
if (empty($post_id)) return;
$post_id = get_the_ID();
$date = get_the_date( \'Y-m-d H:i:s\', $post_id );
$cat = array();
$tag = array();
现在我们需要获得我们帖子的所有类别。
if (!$ignore_categories) {
$post_categories = wp_get_post_terms( $post_id, \'category\' );
if (!empty($post_categories) && is_array($post_categories)) {
foreach ($post_categories as $category) {
$cat[] = $category->term_id;
}
$cat = implode(\',\',$cat);
}
}
现在我们需要获取我们帖子的所有post\\u标签。
if (!$ignore_tags) {
$post_tags = wp_get_post_terms( $post_id );
if (!empty($post_tags) && is_array($post_tags)) {
foreach ($post_tags as $post_tag) {
$tag[] = $post_tag->term_id;
}
$tag = implode(\',\', $tag);
}
}
因为Wordpress还不支持这种查询,所以我们必须重建基本查询并使其适应我们的需要。
global $wpdb, $post;
$innerjoin = array();
$where = array();
if (!empty($cat)) {
$innerjoin[]= "INNER JOIN $wpdb->term_relationships AS tt1 ON ($wpdb->posts.ID = tt1.object_id)";
$where[] = "tt1.term_taxonomy_id IN ($cat)";
}
if (!empty($tag)) {
$innerjoin[]= "INNER JOIN $wpdb->term_relationships AS tt2 ON ($wpdb->posts.ID = tt2.object_id)";
$where[] = "tt2.term_taxonomy_id IN ($tag)";
}
$innerjoin = implode("\\n", $innerjoin);
if (!empty($where))
$where = \'WHERE (\'.implode(" AND \\n", $where).\') AND\';
else
$where = "WHERE";
$query = "SELECT
$wpdb->posts.ID,
$wpdb->posts.post_date as DateOrder,
2 SortOrder
FROM $wpdb->posts
{$innerjoin}
{$where} $wpdb->posts.ID = {$post_id}
UNION ALL
(SELECT
$wpdb->posts.ID,
$wpdb->posts.post_date,
3
FROM $wpdb->posts
{$innerjoin}
{$where}
$wpdb->posts.post_type = \'post\'
AND $wpdb->posts.post_status = \'publish\'
AND $wpdb->posts.post_date > \'{$date}\'
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date ASC LIMIT $quantity)
UNION ALL
(SELECT
$wpdb->posts.ID,
$wpdb->posts.post_date,
1
FROM $wpdb->posts
{$innerjoin}
{$where} $wpdb->posts.post_type = \'post\'
AND $wpdb->posts.post_status = \'publish\'
AND $wpdb->posts.post_date < \'{$date}\'
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date DESC LIMIT $quantity)
ORDER BY SortOrder ASC, DateOrder DESC";
我在上找到了如何在特定行之前和之后获取行的基本知识
stackoverflow 并对排序进行了说明
there. 为了了解当前版本的WordPress(v4.4)如何处理查询,我使用构建了一个自定义查询
tax_query 参数并回显SQL。
现在我们需要遍历找到的帖子。
$loop = $wpdb->get_results($query);
if ( $loop ) {
foreach ( $loop as $row ) {
$post = get_post($row->ID);
setup_postdata( $post );
?>
<div class="three-titles">
<a href="<?php the_permalink() ?>"<?php echo (get_the_ID() == $post_id?\' class="current"\':\'\')?>><?php the_title(); ?></a></br>
</div>
<?php
}
}
wp_reset_postdata();
}
我们得到了结果。要获取相关帖子,请执行以下操作:
环路内部:the_assoc_posts();
循环外部:the_assoc_posts($post_id);
最多5个相关职位:the_assoc_posts($post_id, 5);
忽略关联类别:the_assoc_posts(null, 3, false);
忽略关联的标记:the_assoc_posts(null, 3, true, false);