title: How to sort Wordpress array with posts by own custom key
body: <p><strong>I'd like to sort an array with posts by own custom key 'expiration_date'</strong></p>
  <pre><code>function kursy_wg_daty() {
      $a = 0;
      $todayDate = date(&quot;Y-m-d&quot;);
  
      echo &quot;&lt;ul&gt;&quot;;
      while($a &lt; 20) {
          if($a == 0) {
              // posts with single expiration_date
              $args = array(&quot;posts_per_page&quot; =&gt; 20, &quot;meta_key&quot; =&gt; 'expiration_date');
              $posts_array = get_posts($args);
              
              foreach($posts_array as $post){
                  $expiration_date = get_post_meta( $post-&gt;ID, 'expiration_date_'.$a, true );
                  $price = get_post_meta( $post-&gt;ID, 'price_'.$a, true );
  
                  $only_date = substr($expiration_date, 0, 10);
                  $time = strtotime($only_date);
                  $newFormatDate = date('Y-m-d', $time);
                                  
                  if ($newFormatDate &gt; $todayDate)
                  {
                      echo &quot;&lt;li&gt; &lt;a class='title' href=&quot; . post_permalink($post) . &quot;&gt;&quot; . $post-&gt;post_title . &quot;&lt;/a&gt; | &quot; . $post-&gt;expiration_date . &quot;, price: &quot; . $post-&gt;price . &quot; zł &quot; . &quot;&lt;/li&gt;&quot;;
                  }
              }
          } else {
              // posts with more than one expiration_date
              $args = array(&quot;posts_per_page&quot; =&gt; 20, &quot;meta_key&quot; =&gt; &quot;expiration_date&quot; . &quot;_&quot; . $a);
              $posts_array = get_posts($args);
              
              foreach($posts_array as $post) {
                  $expiration_date = get_post_meta( $post-&gt;ID, 'expiration_date_'.$a, true );
                  $price = get_post_meta( $post-&gt;ID, 'price_'.$a, true );
                  
                  $only_date = substr($expiration_date, 0, 10);
                  $time = strtotime($only_date);
                  $newFormatDate = date('Y-m-d', $time);
                                  
                  if ($newFormatDate &gt; $todayDate)
                  {
                      echo &quot;&lt;li&gt; &lt;a class='title' href=&quot; . post_permalink($post) . &quot;&gt;&quot; . $post-&gt;post_title . &quot;&lt;/a&gt; | &quot; . $expiration_date . &quot;, price: &quot; . $price . &quot; zł &quot; . &quot;&lt;/li&gt;&quot;;
                  }
              }
          }
          $a++;
      }
      echo &quot;&lt;/ul&gt;&quot;;
  }
  </code></pre>
  <p><strong>Now the effect looks like:</strong></p>
  <ul>
  <li>Kurs A | 2021-04-10 (08:30-9:45), price: 190 zł</li>
  <li>Kurs B | 2021-04-09 (12:00-13:00), price: 359 zł</li>
  <li>Kurs C | 2021-04-16 (08:30-9:45), price: 150 zł</li>
  <li>Kurs B | 2021-04-15 (09:00-11:00), price: 399 zł</li>
  <li>Kurs D | 2021-04-08 (11:00-12:30), price: 370 zł</li>
  <li>Kurs D | 2021-04-12 (11:00-12:30), price: 230 zł</li>
  <li>Kurs D | 2021-04-17 (09:00-11:00), price: 180 zł</li>
  </ul>
  <p><strong>but it should be sorted by 'expiration_date' - so in my code it is $newFormatDate, like this:</strong></p>
  <ul>
  <li>Kurs D | 2021-04-08 (11:00-12:30), price: 370 zł</li>
  <li>Kurs B | 2021-04-09 (12:00-13:00), price: 359 zł</li>
  <li>Kurs A | 2021-04-10 (08:30-9:45), price: 190 zł</li>
  <li>Kurs D | 2021-04-12 (11:00-12:30), price: 230 zł</li>
  <li>Kurs B | 2021-04-15 (09:00-11:00), price: 399 zł</li>
  <li>Kurs C | 2021-04-16 (08:30-9:45), price: 150 zł</li>
  <li>Kurs D | 2021-04-17 (09:00-11:00), price: 180 zł</li>
  </ul>
  <p>This question is associated with my previous question <a href="https://stackoverflow.com/questions/66843843/how-to-display-all-wordpress-posts-with-own-custom-key">How to display all Wordpress posts with own custom key</a></p>