0

My code:

    if ($sri) {
      $sri   = intval($sri);
      $args  = [
          'post_type'   => 'notification',
          'numberposts' => -1,
          'meta_query'  => [
              'relation' => 'AND',
              [
                  'key'     => 'send_at',
                  'value'   => Ut::getMysqlTimestamp('now'),
                  'compare' => '<=',
              ],
              //            array(
              //                'key'     => 'was_sent',
              //                'value'   => '1',
              //                'compare' => '=',
              //            ),
              [
                  'key'     => 'related_post_id',
                  'value'   => $sri,
                  'compare' => '='
              ],
              [
                  'key'     => 'notification_type',
                  'value'   => Notification::STORY_REQUEST_MAIN_TYPE,
                  'compare' => '='
              ],
//              [
//                  'relation' => 'OR',
//                  [
//                      'key'     => 'related_post_id2',
//                      'value' => '',
//                      'compare' => 'NOT EXISTS',
//                  ],
//                  [
//                      'key'     => 'related_post_id2',
//                      'value'   => '',
//                      'compare' => '='
//                  ],
//                 [
//                      'key'     => 'related_post_id2',
//                      'value'   => null,
//                      'compare' => '='
//
//                  ],
//                  [
//                      'key'     => 'related_post_id2',
//                      'value'   => 'NULL',
//                      'compare' => '='
//
//                  ]
//              ]
          ],
          'meta_key'    => 'send_at',
          'orderby'     => 'meta_value',
          'order'       => 'DESC'
      ];
      $posts = get_posts($args);
      d($posts);
      die('bb');

the commented out part of the code doesn't work

even though the value is clearly NULL

Wordpress is of 2020 so not old... version 5.3

enter image description here

  • Does this answer your question? Search Custom Post with meta_value NULL – Jacob Peattie Feb 13 at 7:54
  • @JacobPeattie no it does seem that EXISTS isnt doing what is written in that post. It says it checks for NULL but that doesnt seem to be the case. – Toskan Feb 14 at 4:40
  • if you debug the query builder you will see that NOT EXISTS checks if there is no meta ROW there. EXISTS checks if there is a meta ROW with whatever value. There is no way to check for null. We now officially welcome wordpress to mickey mouse programming. – Toskan Feb 14 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.