wordpress Duplicate Queries问题:如何避免不必要重复查询代码示例

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

以下代码仅供参考:
1、查询指定文章时,用post__not_in进行排除,重点在wp_pluck_list方法
$post_ids = wp_pluck_list($wp_query->posts, 'ID');
$args = array(
'posts_per_page' => 1,
'cat' => 1,
'post__not_in' => $post_ids
);
$q = new WP_Query($args);

if ($q->have_posts()) {
while($q->have_posts()) {
$q->the_post();

//Your loop

}
wp_reset_postdata();
}

2、在两个分类里查询文章(两个分类有重复文章),避免重复文章代码示例
在下面代码中,我们将帖子 ID 存储在一个数组中。之后,我们在第二个查询中添加了参数,以排除第一个循环中显示的帖子。$excludepost__not_in


代码如下:

'news'
'posts_per_page' => 3
) );
// The Loop
if ( $first_query->have_posts() ) {
echo '

    ';
    while ( $first_query->have_posts() ) {
    $first_query->the_post();
    // Store Post IDs in an Array to reuse later
    $exclude[] = $post->ID;
    //display posts
    echo '

  • '
    echo the_post_thumbnail( array(50, 50) );
    echo get_the_title();
    echo '
  • ';
    }
    echo '

';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
/****** The Second Query *******/
$second_query = new WP_Query( array (
'category_name' => 'travel',
'post__not_in' => $exclude, // Tell WordPress to Exclude these posts
'posts_per_page' => 3
) );
// The Loop
if ( $second_query->have_posts() ) {
echo '

    ';
    while ( $second_query->have_posts() ) {
    $second_query->the_post();
    echo '

  • ';
    echo the_post_thumbnail( array(50, 50) );
    echo get_the_title();
    echo '
  • ';
    }
    echo '

';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

3、另一段有用的代码
Using instead of for this page.new WP_Queryquery_posts
Defining the query variable () as global.$main_query
Querying a temp array () with my featured posts.$temp_featured
Creating an array with only the IDs of the `$temp_featured’. Note EAMann’s use of the wp_list_pluck function.
Executing the main query of the page () with the argument to exclude the IDs retrieved in #4.$main_query

global $main_query;

$temp_featured = get_posts(
array(
'post_type' => 'custom_post',
'custom_post-category' => 'featured-cat',
'posts_per_page' => 2)
);
$featured_ids = wp_list_pluck( $temp_featured, 'ID' );

$query_args = array(
'post_type' => 'custom_post',
'posts_per_page' => $per_page,
'paged' => $current_page,
'post__not_in' => $featured_ids
);
$main_query = new WP_query ($query_args);

//displaying the two featured posts with their own query
$featured = new WP_query( array(
'post_type' => 'custom_post',
'custom_post-category' => 'featured-cat',
'posts_per_page' => 2)
);
while ($featured->have_posts ()) : $featured->the_post();
the_title();
the_excerpt();
endwhile;

//displaying the full query of the page
if ($main_query->have_posts ()) :
while ($main_query->have_posts ()) : $main_query->the_post();
the_title();
the_excerpt();
endwhile;
endif;

4、另一端有参考意义的查询前进行判断的代码

if ( $use_global_query ) {
global $wp_query;
if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) {
// Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination.
unset( $query_args['offset'] );
$query_args = wp_parse_args( $wp_query->query_vars, $query_args );

if ( empty( $query_args['post_type'] ) && is_singular() ) {
$query_args['post_type'] = get_post_type( get_the_ID() );
}
}
$query = $wp_query;
} else {
$query = new WP_Query( $query_args );
}

if ( ! $query->have_posts() ) {
return '';
}