0

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!

4
  • what's the problem with array_merge? And i did not come up with any use of + operator in merging to array ? Can you provide some info regarding this? Commented Oct 1, 2012 at 18:50
  • I was using a tip from Example 3 on php.net/manual/en/function.array-merge.php for the "+" operator, if I use array_merge() then "args2" overwrites "args" as they share key names. I'm aiming to split the result of the query: (1) One side having post dates > today (active) (2) Other side having post dates < today (expired) I then want to show qualifying posts of (1), then show posts of (2) below them whilst respecting the pagination (post_per_page => 5). Commented Oct 1, 2012 at 20:38
  • can you share your desired array in a var_dump format ? Anyway that info was useful :) Commented Oct 1, 2012 at 21:06
  • To be honest I'm not sure how it'd look in a var_dump, I'm hacking this together trying to make it work as it is! I'm just trying to get the Query to sort posts based on the 'expired' meta_key value, placing the posts with a date after "$today" at the top then with a date before "$today" after them, so the post is still there but as it has 'expired' it doesn't need to be at the top anymore! This is assuming arrays are the right method to even do this, I'm pretty sure they are. Commented Oct 1, 2012 at 21:22

2 Answers 2

0

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 :)

Sign up to request clarification or add additional context in comments.

3 Comments

That's a fantastic help, thanks so much for explaining. Your solution advice also makes sense so I'm going to start testing and see where I get. Will update this when/if I get to what I need. Thanks again.
In the end I re-organised the page that will display these posts and call the two arrays individually - giving a slightly different result but one I'm actually quite happy with. Thanks again, you pushed me in a better direction.
It's great to hear that it helped it anyway ;)
0

You can't just add 2 arrays together using the args+args2 syntax. PHP has no way of knowing what you mean by that. If you want the result to be an array containing both of these arrays, you could do this:

$final = array($args, $args2)

Otherwise I'm not sure how you want these two arrays combined. If you clarify how they need to be combined we might be able to help more.

2 Comments

See my comment above, I'm looking to split the query results into posts that have expired and those that 'expire' at a future date. Then I need to first display all of the non-expired posts at the top of the page, once that is exhausted to then begin a while loop on the second array to display the expired ones.
I stand corrected on the + operator for arrays, but in your case the + operator shouldn't do anything. All the keys in args2 already exist in args, so they will not be overwritten. Can you give us some indication of what you want $final to look like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.