php - Add incremental parameter value to each post in wordpress query loop -
i'm trying combine several loops 1 , sort final results relevance.
for former part, did this:
// set 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 same author $by_author = new wp_query (array( 'author' => $author_id, 'posts_per_page' => '5' )); // add ids array if ($by_author->have_posts()) { while ($by_author->have_posts()) { $by_author->the_post(); $add[] = get_the_id(); } } // loop same tag $by_tag = new wp_query(array( 'tag__in' => $first_tag, 'posts_per_page' => '5' )); // add ids array if ($by_tag->have_posts()) { while ($by_tag->have_posts()) { $by_tag->the_post(); $add[] = get_the_id(); } } // loop same category $by_category = new wp_query(array( 'category__in' => $categories_id, 'posts_per_page' => '5' )); // add ids 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] } }
this working nicely combining loops one. latter part, i'm trying next add incremental "weight" value each post come later sort them 'orderby' => $weight
,.
for example, if post comes in "same author" gets 3 points, if 1 comes in same tag gets 2 points , on. if comes in more 1 loop, should combined points i.e. 3+2+1=6 hence boosted top of final query.
i have tried add counter each preliminary loop, $weight = +3
etc, adds every post, not individually.
i tried inserting @ end of each preliminary loop...
$weight = 0; if ($by_author){ foreach ($by_author $post){ setup_postdata($post); $weight = +10; add_post_meta($post->id, 'incr_number', $weight, true); update_post_meta($post->id, 'incr_number', $weight); } }
... , final one
echo get_post_meta($post->id,'incr_number',true);
but still doesnt right. assigns global value, while want them different depending on actual main posts 1 reading.
so there way this?
if understand question right, think last solution close. instead of global $weight
parameter, however, think need build array of $weights
that's unique each post:
$weights[$post.id] += 10;
then, sort array , heavily weighted post ids there.
Thanks for give us valuable information
ReplyDeleteContact WordPress Support
WordPress Support Service