在wordpress查询循环中为每个帖子添加增量参数值

时间:2016-07-19 13:15:50

标签: php wordpress loops foreach parameters

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

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

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

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

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题,我认为你的最后解决方案很接近。但是,我认为您需要为每个帖子构建一个 <table> <tr> <td> <div id="filterContainer"> <ul id="filters"> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="normalPic"> <a href="#" id="normal">Normal</a> </li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="vintagePic"> <a href="#" id="vintage">Vintage</a> </li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="lomoPic"> <a href="#" id="lomo">Lomo</a> </li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="clarityPic"> <a href="#" id="clarity">Clarity</a> </li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="sinCityPic"> <a href="#" id="sinCity">Sin City</a> </li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="sunrisePic"> <a href="#" id="sunrise">Sunrise</a> </li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="crossProcessPic"> <a href="#" id="crossProcess">Cross Process</a> </li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="orangePeelPic"> <a href="#" id="orangePeel">Orange Peel</a></li> <li><img src="R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" id="lovePic"> <a href="#" id="love">Love</a></li> <li><img src="R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" id="grungyPic"> <a href="#" id="grungy">Grungy</a></li> <li><img src="R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" id="jarquesPic"> <a href="#" id="jarques">Jarques</a></li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="pinholePic"> <a href="#" id="pinhole">Pinhole</a></li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="oldBootPic"> <a href="#" id="oldBoot">Old Boot</a></li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="glowingSunPic"> <a href="#" id="glowingSun">Glowing Sun</a></li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="hazyDaysPic"> <a href="#" id="hazyDays">Hazy Days</a></li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="herMajestyPic"> <a href="#" id="herMajesty">Her Majesty</a></li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="nostalgiaPic"> <a href="#" id="nostalgia">Nostalgia</a></li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="hemingwayPic"> <a href="#" id="hemingway">Hemingway</a></li> <li><img src="http://vignette1.wikia.nocookie.net/farmville2/images/3/36/Pink_Lemonade_Blueberry_Cupcake.png/revision/latest?cb=20150927002728" id="concentratePic"> <a href="#" id="concentrate">Concentrate</a></li> </ul> </div> </td> </tr> </table> #filterContainer ul{ width: 30%; display: inline-block; white-space: nowrap; overflow: auto; margin: 0; padding: 0; } #filterContainer li{ width: 25%; display: inline-block; } #filterContainer{ margin-top: 1%; display: block; margin: 0 auto; } 数组,而不是全局$weight参数:

$weights

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