Search code examples
phpmysqlarraysmerge

Appending one array to another array in the same loop


This is for a custom Wordpress page but I think the basic array principles should apply. I've not worked with complex arrays before so am a little lost, trial and error hasn't worked yet.

I have a database of Posts, each post has meta_key's of 'shop' and 'expired'. 'expired' is a date (YYYY-MM-DD) which is used to tell the visitor when a Post's content expires and this key is what I'm trying to work with.

If a Post's 'expired' date is before today, they are shown 'Offer expired'

If the 'expired' date is in the future, they are shown 'Offer expires in X days' (this script isn't shown below, not necessary)

Posts are listed in order of their 'expired' date, ASC. The problem is that when a post expires I'd like that post to show at the end rather than stay on top.

Example of what I currently see:

Post 1 | Expired 3 days ago
Post 2 | Expired 1 day ago
Post 3 | Expires in 2 days
Post 4 | Expires in 6 days

And what I'd like to see (note Post X order):

Post 3 | Expires in 2 days
Post 4 | Expires in 6 days
Post 2 | Expired 1 day ago
Post 1 | Expired 3 days ago

This is my array code where I've attempted to merge the two

$postid = get_the_ID();
$meta1 = get_post_meta($postid, 'shop', true);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$today = date('Y-m-d', time());

$args = array(
'post_type' => 'post',
'meta_query' => array(
    array(
        'key' => 'shop',
        'value' => $meta1
    )
),
'paged' => $paged,
'posts_per_page' => '5',
'meta_key' => 'expired',
'meta_value' => $today,
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'ASC'
 );

$args2 = array(
'post_type' => 'post',
'meta_query' => array(
    array(
        'key' => 'shop',
        'value' => $meta1
    )
),
'meta_key' => 'expired',
'meta_value' => $today,
'meta_compare' => '<',
'orderby' => 'meta_value',
'order' => 'DESC'
);

$final = $args + $args2;
$query = new WP_Query( $final );

while ( $query->have_posts() ) : $query->the_post(); ?>
HTML FOR DISPLAYING POST
endwhile;

At the moment it doesn't seem to take any notice of "$args2" and only displays $args

I'm sure my idea is on the right lines, needing to create two arrays and join them with the "+" rather than array_merge() but that's where I can't get any further.

Can someone kindly shed some light please? Thanks!


Solution

  • Now the solution you are trying to achieve is actually impossible if i understood your requirement properly. Let me explain why this is not achievable.

    In your two arrays $args and $args2 most of the values are same leaving two odds , i am picking only one to just illustrate :

    //it's in args    
    'meta_compare' => '>'
    //it's in args2
    'meta_compare' => '<'
    

    Now what happens when you are trying to merge this two using array_merge($args , $args2):

    'meta_compare' => '<'
    

    That means it is taking 'meta_compare' from the later array which is $args2 here. This is the behavior of array_merge function defined in the doc:

    If the input arrays have the same string keys, then the later value for that key will overwrite the previous one

    Now if you are using + array union operator $args+$args2 :

    'meta_compare' => '>'
    

    That means it is taking 'meta_compare' from the first array which is $args here. This is the behavior of + array union operator defined in the doc :

    The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

    Why it is happening because in the same level keys have to be unique . So in your situation they are in the same level :

    $args = array ('key' => 'value1')
    $args2= array ('key' => 'value2')
    

    So only and only one value can exist here as in the merged array 'key' can point only one.

    If this was the scenario :

    $args [0] = array ('key' => 'value1' ) 
    $args2 [1]= array ('key' => 'value2' ) 
    

    Then both of the value stored by key will be resrved. This will be the resulting array :

    array(2) {
      [0]=>
      array(1) {
        ["key"]=>
        string(6) "value1"
      }
      [1]=>
      array(1) {
        ["key"]=>
        string(6) "value2"
      }
    }
    

    Though i am not a geek of WP but as far as i understood from WP_query you can't pass args like this (I am not sure). So that leaves only one way to achieve this. You can pass these two args separately and merge the result as the result most probably (I am not sure) will be a 2D array you can merge them easily.

    Hope that helps. Happy coding :)