gpt4 book ai didi

wordpress - 多个查询的一个分页(或如何对一个查询进行分组)

转载 作者:行者123 更新时间:2023-12-03 07:16:38 25 4
gpt4 key购买 nike

我在同一页面中有多个查询,如下所示:

$args_1 = array (
'category_name' => get_query_var( 'category_name' ),
'post__in' => $sticky = get_option( 'sticky_posts' );
'orderby' => 'date',
'order' => 'DESC'
);

$sticky_query = new WP_Query ($args_1);

// loop


$args_2 = array(
'category_name' => get_query_var( 'category_name' ),
'post__not_in' => get_option( 'sticky_posts' ),
'category__not_in' => array(11114),
'orderby' => 'date',
'order' => 'DESC'
);

$query_2 = new WP_Query ($args_2);

// loop



$args_3 = array(
'category_name' => get_query_var( 'category_name' ),
'post__not_in' => get_option( 'sticky_posts' ),
'category__in' => array(11114),
'orderby' => 'date',
'order' => 'DESC'
);


$query_3 = new WP_Query($args_3 );

// loop

我愿意:

1) 将每页帖子总数限制为 15

2)(我不能这样做,我已经搜索过任何地方,但没有找到解决方案)使这个“组合”分页起作用。现在,这是合乎逻辑的,第二页与第一页相同......

或者解决方案可以是仅进行一个查询并对帖子进行分组并排序组?

最佳答案

最终解决方案

  1. 如果合并两个查询并且不使用“偏移”,则不需要自定义分页,因此我已将其删除

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
    ) );

并补充道:

    echo paginate_links(); 
  • 您必须设置 'order''post__in'值以保持单独查询中帖子的顺序。

  • 为了避免由包含许多帖子的查询引起的内存错误(例如“ fatal error :允许的内存大小 134217728 字节耗尽(尝试分配 262144 字节)”。),您必须仅检索帖子 ID在查询中。 See here.

  • 这是最终的代码:

        global $wp_query;



    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $post_per_page = 35; // How many post per page - setup as you need
    $nr_of_posts_to_be_analyzed_to_include_only_current_events = 100; // How many post to analyze to include only current events, ie events that are not in category id 11114



    $sticky = get_option( 'sticky_posts' );

    // posts in category, limited in number otherwise - if you set '-1' and you have 7.000 posts - you exceed the script time limit execution
    $category_posts = get_posts(array('category_name' => get_query_var( 'category_name' ), 'posts_per_page' => $nr_of_posts_to_be_analyzed_to_include_only_current_events,'orderby' => 'date', 'order' => 'DESC'));

    $category_posts_ids = array();

    foreach( $category_posts as $post ) {

    $category_posts_ids[]=$post->ID; // Array with posts ID

    }
    // var_dump($category_posts_ids);


    // expired posts in category, limited in number otherwise - if you set '-1' and you have 7.000 posts - you exceed the script time limit execution
    $expired_posts = get_posts(array('category__and' => array(11114, get_query_var('cat')), 'posts_per_page' => $nr_of_posts_to_be_analyzed_to_include_only_current_events, 'orderby' => 'date', 'order' => 'DESC'));

    $expired_posts_ids = array();

    foreach( $expired_posts as $post ) {

    $expired_posts_ids[]=$post->ID; // Array with posts ID

    }



    // first 100 posts in this category per page - the expired posts that are in 100 first posts in this category gives as result an array of not expired posts
    $array_where_to_get_post = array_diff( $category_posts_ids, $sticky, $expired_posts_ids );



    // 1/3 only the sticky posts
    $stickies_posts_args = array(
    'category_name' => get_query_var( 'category_name' ),
    'post__in' => get_option( 'sticky_posts' ),
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => -1,
    'fields' => 'ids' // important: to avoid memory exhausted error we retrieve postids only
    );

    // 2/3 only the not expired and not sticky posts
    $not_expired_posts_args = array(
    'post__in' => $array_where_to_get_post,
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => -1,
    'fields' => 'ids', // important: to avoid memory exhausted error we retrieve postids only,
    'suppress_filters' => false
    );

    // 3/3 only the expired and not sticky posts
    $expired_posts_args = array(
    'category_name' => get_query_var( 'category_name' ),
    'post__not_in' => get_option( 'sticky_posts' ), // not sticky posts
    'category__in' => array(11114), // expired posts
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => -1,
    'fields' => 'ids' // important: to avoid memory exhausted error we retrieve postids only
    );


    $firstQuery = get_posts($stickies_posts_args);

    add_filter('posts_join','join_posts_and_events'); // apply some filter
    add_filter('posts_orderby', 'order_posts'); // apply some filter
    $secondQuery = get_posts($not_expired_posts_args);
    remove_filter('posts_join','join_posts_and_events');
    remove_filter('posts_orderby', 'order_posts');

    $thirdQuery = get_posts($expired_posts_args);


    $mergePosts = array_merge( $firstQuery, $secondQuery, $thirdQuery ); // Merge all queries

    $uniquePosts = array_unique($mergePosts); // Create an array with unique posts

    // Final query
    $args = array(
    'post_type' => 'any',
    'post__in' => $uniquePosts,
    'paged' => $paged,
    'orderby' => 'post__in', // order the posts as they are (already ordered)
    'order' => 'DESC',
    'posts_per_page' => $post_per_page,
    'ignore_sticky_posts' => 1 // otherwise it breaks pagination
    );

    $wp_query = new WP_Query($args);

    if ( $wp_query->have_posts() ) :

    while ( $wp_query->have_posts() ) { $wp_query->the_post();

    // outputs what you want

    endwhile;

    endif;

    echo paginate_links();

    关于wordpress - 多个查询的一个分页(或如何对一个查询进行分组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41624577/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com