我想创建一个由多个查询的合并结果组成的数组。
我提出了类似的问题,几乎达到了我的预期,但我最终发现这个方法不适合设置分页。 如何按日期对包含通过合并 get_posts 结果创建的 WP post 对象的数组进行排序?
我喜欢设置分页,所以我更喜欢WP_Query方法而不是get_posts方法。
var_dump($args)
显示了我所期望的,但是var_dump之后的new WP_Query( $args )
没有显示任何内容。
请有人添加一些东西以使其正常工作。
<?php
$args_a = get_posts( array(
'suppress_filters' => false,
'category_name' => 'shinmatsudo',
'category__in' => array( 227 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
//etc..
),
));
$args_b = get_posts( array(
'suppress_filters' => false,
'category_name' => 'matsudo',
'category__in' => array( 329 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'and',
array(
'key'=> '2a',
'value' => array('2020-02-01' , '2020-06-01'),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
//etc..
),
));
$args = array( //Maybe this is not working
'paged' => $paged,
'posts_per_page' => 4,
'orderby' =>'date',
);
$args = array_merge($args_a, $args_b );
query_posts( $args );
var_dump($args); //showing expected result
?>
<?php $the_query = new WP_Query($args);?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title ?>
<?php endwhile; wp_reset_query();?>
<?php endif; wp_reset_query();?>
@disinfor 感谢您的代码和评论。作为参考确实有帮助。
我尝试了你的代码,但没有显示任何内容,但是
var_dump($args)
显示了所有合并的帖子信息。
我想我的代码和你的代码都让
$args
合并了帖子信息,但是new WP_Query($args)
没有,这就是这段代码的重点。
总之,关于覆盖,我试图通过
$args_a
来控制$args_b
和$args
。我的意思是将 $args_a and b
存储到 $args
中,然后在 $args
本身中对它们进行排序。也许这是错误的思维方式?
再次查看您的代码,您正在覆盖您的
$args
变量 - 您没有得到预期的结果,因为您的分页从未被调用。
看看你有什么:
$args = array( //Maybe this is not working
'paged' => $paged,
'posts_per_page' => 4,
'orderby' =>'date',
);
$args = array_merge($args_a, $args_b );
query_posts( $args );
var_dump($args); //showing expected result
您正在用
$args
覆盖 posts_per_page
。试试这个:
array_merge()
编辑:
$args_c = array( //Maybe this is not working
'paged' => $paged,
'posts_per_page' => 4,
'orderby' =>'date',
);
// MERGE ALL THE ARGS HERE
$args = array_merge($args_a, $args_b, $args_c );
?>
<?php $the_query = new WP_Query($args);?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title ?>
<?php endwhile; wp_reset_query();?>
<?php endif;?>
来处理这个问题。试试这个:
pre_get_posts
然后在
// Use your original args, but add a parameter.
$args = array_merge($args_a, $args_b, [ 'custom_query' => TRUE ] );
?>
<?php $the_query = new WP_Query($args);?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title ?>
<?php endwhile; wp_reset_query();?>
<?php endif;?>
中添加:
functions.php
这是我参考的网址。
https://wordpress.stackexchange.com/questions/142603/merged-two-wp-queries-posts-per-page-and-pagination-not-working?newreg=70dbebb78f9946658d28d580fb55c8c0add_filter( 'pre_get_posts', static function( $query ) {
// Check to see if 'custom_query' is set to TRUE.
if ( ! is_admin() && $query->custom_query ) {
$query->set( 'paged', TRUE );
$query->set( 'posts_per_page', 4 );
$query->set( 'orderby', 'date' );
}
});
这些是我这些天失败的一些代码。有人请给我提示。失败代码#1
此代码仅显示最新帖子的 1 个标题。
在页面上
<?php
$args_a = get_posts(
array(
'fields' => 'ids',
'posts_per_page' => -1,
'paged' => $paged,
'category_name' => 'shinmatsudo',
'category__in' => array( 227 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
array(
'key' => '1d',
'compare' => 'NOT EXISTS'
),
//etc
),
));?>
<?php
$args_b = get_posts(
array(
'fields' => 'ids',
'category_name' => 'matsudo',
'posts_per_page' => -1,
'paged' => $paged,
'category__in' => array( 329 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
array(
'key' => '1d',
'compare' => 'NOT EXISTS'
),
//etc
),
)); ?>
<?php $post_ids = array_merge( $args_a, $args_b); ?>
<?php
$the_query = new WP_Query(
array(
'post_type' => 'any',
'post__in' => $post_ids,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 9,
)
);
?>
//I set my pagination for page here. I think any place is ok.
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title() ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
在 function.php 上
<?php
$args_a = get_posts( array(
'suppress_filters' => false,
'category_name' => 'shinmatsudo',
'category__in' => array( 227 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
//etc..
),
));
$args_b = get_posts( array(
'suppress_filters' => false,
'category_name' => 'matsudo',
'category__in' => array( 329 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'and',
array(
'key'=> '2a',
'value' => array('2020-02-01' , '2020-06-01'),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
//etc..
),
));
// Use your original args, but add a parameter.
$args = array_merge($args_a, $args_b, [ 'custom_query' => TRUE ] );
?>
<?php $the_query = new WP_Query( $args );?>
<?php include("pagination-page.php")?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title() ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
我尝试过没有像这样的
add_filter( 'pre_get_posts', static function( $query ) {
// Check to see if 'custom_query' is set to TRUE.
if ( ! is_admin() && $query->custom_query ) {
$query->set( 'paged', TRUE );
$query->set( 'posts_per_page', 4 );
$query->set( 'orderby', 'date' );
}
});
。此代码仅显示 $args_b 的结果。
get_posts
失败代码#3我使用带有 pre_get_posts 的自定义查询。 结果与代码#2 相同。 $args_b 仅显示为结果。
在页面上
<?php
$args_a = array(
'posts_per_page' => 10,
'category_name' => 'shinmatsudo',
'category__in' => array( 227 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
//etc..
),
);
$args_b = array(
'posts_per_page' => 10,
'category_name' => 'matsudo',
'category__in' => array( 329 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'and',
array(
'key'=> '2a',
'value' => array('2020-02-01' , '2020-06-01'),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
//etc..
),
);
$args = array_merge($args_a, $args_b );
?>
<?php $the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title() ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
在 Function.php 上
<?php
$args_a = array(
'posts_per_page' => 10,
'category_name' => 'shinmatsudo',
'category__in' => array( 227 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
//etc..
),
);
$args_b = array(
'posts_per_page' => 10,
'category_name' => 'matsudo',
'category__in' => array( 329 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'and',
array(
'key'=> '2a',
'value' => array('2020-02-01' , '2020-06-01'),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
//etc..
),
);
// Use your original args, but add a parameter.
$args = array_merge($args_a, $args_b, [ 'custom_query' => TRUE ] );
?>
<?php $the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title() ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>