여러 루프를 하나로 결합하여 최종 결과를 관련성별로 정렬하려고합니다. 전자의 경우 WordPress 쿼리 루프의 각 게시물에 증분 매개 변수 값을 추가하십시오.
, 내가 한이 : 이것은 잘 작동 하나에 루프를 결합한다// 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
등의 각 예비 루프에 카운터를 추가하려고했으나 개별 게시물이 아닌 모든 게시물을 모두 추가합니다. 마지막 하나
echo get_post_meta($post->ID,'incr_number',true);
하지만 여전히 나던에
나는 또한 각 예비 루프의 끝 부분에 이런 식으로 뭔가를 삽입하려고 ...
$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를 키로하고 값을 가중치로 배열을 작성하기 만하면 점수를 올바르게 할당하는 데 필요한 모든 작업이 완료됩니다. 그래도 일을 정렬하는 데 어려움을 겪고 있습니다. –