【问题标题】:Add incremental parameter value to each post in wordpress query loop在 wordpress 查询循环中为每个帖子添加增量参数值
【发布时间】:2016-07-19 13:15:50
【问题描述】:

我正在尝试将多个循环合并为一个,并按相关性对最终结果进行排序。

对于前一部分,我是这样做的:

// set the variables
$author_id     = get_the_author_meta('ID');
$tags_id       = wp_get_post_tags($post->ID);
$first_tag     = $tags_id[0]->term_id;
$categories_id = wp_get_post_categories($post->ID);

// loop for same author
$by_author = new WP_Query (array(
    'author'         => $author_id,
    'posts_per_page' => '5'
));

// add ids to array
if ($by_author->have_posts()) {
    while ($by_author->have_posts()) {
        $by_author->the_post();
        $add[] = get_the_id();
    }
}

// loop for same tag
$by_tag = new WP_Query(array(
    'tag__in'        => $first_tag,
    'posts_per_page' => '5'
));

// add ids to array
if ($by_tag->have_posts()) {
    while ($by_tag->have_posts()) {
        $by_tag->the_post();
        $add[] = get_the_id();
    }
}

// loop for same category
$by_category = new WP_Query(array(
    'category__in'   => $categories_id,
    'posts_per_page' => '5'
));

// add ids to array
if ($by_category->have_posts()) {
    while ($by_category->have_posts()) {
        $by_category->the_post();
        $add[] = get_the_id();
    }
}

// loop array of combined results 
$related = new WP_Query(array(
    'post__in'       => $add,
    'post__not_in'   => array($post->ID),
    'posts_per_page' => '10',
    'orderby' => $weight[$post->ID],
    'order'   => 'DESC'
));

// show them
if ($related->have_posts()) {
    while ($related->have_posts()) {
        $related->the_post();
        // [template]
    }
}

这很好地将循环组合为一个。对于后一部分,我接下来要做的是在每个帖子出现时为每个帖子添加一个递增的“权重”值,以便稍后使用 'orderby' => $weight 之类的东西对它们进行排序。

例如,如果一个帖子出现在“同一作者”中,则获得 3 分,如果另一个帖子出现在同一标签中,则获得 2 分,以此类推。如果它出现在多个循环中,它应该得到组合点,即 3+2+1=6,因此被提升到最终查询的顶部。

我尝试为每个初步循环添加一个计数器,例如 $weight = +3 等,但这只会为每个帖子添加所有内容,而不是单独添加。

我还尝试在每个初步循环的末尾插入类似的内容...

$weight = 0;
if ($by_author){
    foreach ($by_author as $post){
        setup_postdata($post);
        $weight = +10;
        add_post_meta($post->ID, 'incr_number', $weight, true);
        update_post_meta($post->ID, 'incr_number', $weight);
    }
}

...这是最后一个

echo get_post_meta($post->ID,'incr_number',true);

但它仍然不正确。它分配了一个全局值,而我希望它们根据正在阅读的实际主要帖子而有所不同。

那么有没有办法做到这一点?

【问题讨论】:

  • 我不建议在循环中进行数据库调用(add_post_metaupdate_post_meta 等)。您的网站很快就会崩溃。
  • 确实,那部分完全是无能和无用的。只需简单地构建以 id 为键、权重为值的数组即可正确分配分数。不过,我仍在努力解决问题。

标签: php wordpress loops foreach parameters


【解决方案1】:

如果我正确理解您的问题,我认为您的最后一个解决方案很接近。但是,我认为您需要构建一个 $weights 数组,而不是全局 $weight 参数,这对每个帖子都是独一无二的:

$weights[$post.id] += 10;

然后,您可以对该数组进行排序并从中获取权重最大的帖子 ID。

【讨论】:

  • 确实,这似乎是要走的路——所以如果我做对了,它所做的就是将帖子 ID 存储为数组中的“键”而不是“值”,其中“值”为重量本身?我仍然很难弄清楚如何在最终查询中解决问题。
  • 是的,这就是我的设想。您可以在此处查看从高到低排序的示例,甚至还有显示如何获取密钥(帖子 ID)的示例:php.net/manual/en/function.arsort.php
  • 显然,我需要做的就是在每个 $weight[$post->ID] += 3; 之后添加一个 $weight[$post->ID] += 3;get_the_id(); 现在它可以正常工作,根本不需要 foreach。排序确实很棘手,'orderby' => $weight[$post->ID] 什么都不做。
  • 是的,通过arsort($weight); foreach ($weight as $key => $value) { $posts_ordered[] $key; }'post__in' => $posts_ordered, 'orderby' => 'post__in' 的帮助,现在可以正常工作了 - 谢谢。
  • “你可能会在 19 小时内奖励你的赏金” - 我想我需要再次回来:D
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 2012-10-17
  • 2017-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多