0

I want to create a single array made by merged results of multiple queries.

I made similar question, and I almost achieved what I expected, but I finally found that this method is not suitable to set pagination. How to sort an array by date that contains WP post objects created by merging get_posts results?

I like to set pagination, so I prefer WP_Query method rather than get_posts method.

var_dump($args) shows what I expected, but new WP_Query( $args ) after var_dump does not show anything.

Somebody please add something to make it work.

<?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();?>
cwhiro
  • 105
  • 7
  • Can you edit your question to tell us what you results you are expecting? In your last question you set only `9` posts per query argument - 9 for `shinmatsudo` and 9 for `matsudo` category terms. Is that not necessary? Also, you have two `wp_reset_query()`. – disinfor Jul 26 '20 at 12:17
  • Hi disinfor Thank you everytime. I found that I could not make pagination by get_posts method, so now I have been trying to achieve it by WP_Query. I deleted post_per_page => 9 because Im wondering 9 posts from $args_a and $args_b might be able to controled by 3rd $args. Maybe Im wrong again... – cwhiro Jul 26 '20 at 12:51
  • 1
    Hi disinfor Thanks for your help. I finally could make it. I show my final code. – cwhiro Jul 30 '20 at 04:53
  • Nice! Glad you got it to work. – disinfor Jul 30 '20 at 11:57

3 Answers3

0

Looking at your code, again, you are overwriting your $args variable - you aren't getting the expected results because your pagination is never being called.

Look at what you have:

$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

You are overwriting the $args with the posts_per_page with the array_merge().

Try this:

$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;?>

EDIT:

Try using a pre_get_posts to handle this. Try this out:

// 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;?>

Then in functions.php add this:

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' );
    }
});
disinfor
  • 10,865
  • 2
  • 33
  • 44
  • Thank you for your code. Thank you for your time for my question. Now I posted my result. – cwhiro Jul 27 '20 at 02:23
  • @cwhiro added a new test to try. – disinfor Jul 27 '20 at 13:21
  • Thank you for adding new code. Now it does not show anything, but I feel Im getting close to right answer by your suggested pre_get_posts function. I try to touch the code, and report you. Thanks a lot ! – cwhiro Jul 27 '20 at 23:32
  • I wrote some of code as new answer. I thought your suggested ` pre_get_posts` is almost answer of my question, but Im still in stacked. Your further help is really appreciated. – cwhiro Jul 29 '20 at 13:31
0

@disinfor Thank you for your code and comment. It does help as a reference.

I tried your code, but nothing is shown however var_dump($args) shows all merged post information.

I think both my code and your code let $args have merged posts information, but new WP_Query($args) does not have it, and this is the point of this code.

Anyway, regarding overwriting, I was trying to control $args_a and $args_b by $args. I mean to store $args_a and b into $args, then sort them in $args itself. Maybe this is wrong way of thinking?

cwhiro
  • 105
  • 7
0

Finally, I could solve my problem. I show my final code for somebody else who stacks by same problem.

This is URL I refered.

https://wordpress.stackexchange.com/questions/142603/merged-two-wp-queries-posts-per-page-and-pagination-not-working?newreg=70dbebb78f9946658d28d580fb55c8c0

Final Code

    <?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(); ?>

These are some of codes I failed these days. Somebody plese give me tips.

Failed Code #1

This code shows only 1 title of the latest post.

On page

<?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(); ?>

On function.php

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' );
    }
});

Failed Code #2

I tried without get_posts like this. This code shows reslut of $args_b only.

<?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(); ?>

Faild Code #3

I use custom query with pre_get_posts. Result is same like Code #2. $args_b only shown as result.

On Page

<?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(); ?>

On Function.php

<?php
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' );
    }
});

?>
cwhiro
  • 105
  • 7