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

以下代码仅供参考:
1、查询指定文章时,用post__not_in进行排除,重点在wp_pluck_list方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$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


代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/******  The First Query *******/
$first_query = new WP_Query(  array (
'category_name' => 'news'
'posts_per_page'  =>  3
) );
// The Loop
if ( $first_query->have_posts() ) {
    echo '<ul>';
    while ( $first_query->have_posts() ) {
        $first_query->the_post();
        // Store Post IDs in an Array to reuse later
        $exclude[] = $post->ID;
        //display posts
        echo '<li>'
        echo the_post_thumbnail( array(50, 50) );
        echo get_the_title();
        echo '</li>';
    }
    echo '</ul>';
} 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 '<ul>';
    while ( $second_query->have_posts() ) {
        $second_query->the_post();
        echo '<li>';
        echo the_post_thumbnail( array(50, 50) );
        echo get_the_title();
        echo '</li>';
    }
    echo '</ul>';
} 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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、另一端有参考意义的查询前进行判断的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    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 '';
    }
原文链接:https://xiaohost.com/11574.html,转载请注明出处。
0