私はいくつかのループを1つに結合し、最終結果を関連性でソートしようとしています。ワードプレスクエリループの各ポストにインクリメンタルパラメータ値を追加します。
元の部分については、私はこれをしなかった:
// 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]
}
}
これはうまく一つにループを組み合わせて取り組んでいます。後者の部分については、私が次にしようとしているのは、それぞれの投稿にインクリメンタルな "weight"値を追加して、後で'orderby' => $weight
のようなもので並べ替えることです。
たとえば、投稿が「同じ著者」に表示された場合は3ポイント、同じタグに別のタグが表示された場合は2ポイントなどとなります。 2つ以上のループに現れた場合、3 + 2 + 1 = 6という結合点を得て、最終的なクエリの先頭に追加する必要があります。
$weight = +3
などのように各予備ループにカウンタを追加しようとしましたが、これは個々のポストごとにすべてを追加するだけです。最後の1
echo get_post_meta($post->ID,'incr_number',true);
しかし、それはまだdoesntのに
私はまた、各予備ループの最後で、このような何かを挿入しようとした...
$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);
}
}
...と、このちゃんとして。それはグローバルな値を割り当てますが、実際のメインポストが読んでいるかどうかによってそれらが異なるようにしたいのです。
これを行う方法はありますか?
ループ内でデータベース呼び出し( 'add_post_meta'、' update_post_meta'など)を行うことはお勧めしません。あなたのサイトは急いで爆破するでしょう。 – adamdport
実際、その部分はまったく役に立たず、役に立たなかった。 idをキーとし、値をweightとして配列を構築するだけで、スコアを適切に割り当てることができます。しかし、まだそれを並べ替えるのに苦労している。 –