Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:28:38+00:00 2026-06-12T04:28:38+00:00

This is for a custom WordPress page but I think the basic array principles

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:28:40+00:00Added an answer on June 12, 2026 at 4:28 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom search engine on a non-wordpress page. This search engine searches
I am running a custom loop in a wordpress page that should list all
I have a custom form in one of my wordpress pages(this page uses a
I have this custom JSlider, which will be used in many other forms/windows. But
We are using a custom page on Tridion 2009. This custom page was working
I created a custom page template to display a filterable portfolio in Wordpress. However,
I currently have a custom page in Wordpress (with the PHP Execution plugin). On
I've already asked this question on wordpress.stackexchange.com but they told me this is a
I am trying to make a php page (It's Wordpresss but mostly custom php)
This is my first attempt at coding a custom wordpress theme and I'm almost

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.