admin管理员组

文章数量:1410716

So I'm trying to target some posts to include in my function but I just can't seem to get it to work.

When I have the meta_query taken out I get 9 results (Works):

$args = array(
    'post_type'      => 'office',
    'post_status'    => 'any',
    'posts_per_page' => -1,
    'date_query' => array(
        array(
            'column' => 'post_modified_gmt',
            'before' => $started,
        ),
    ),
);
$the_query = new WP_Query($args);

Then I get one result as below:

object(WP_Post)[1306]
  public 'ID' => int 50794
  public 'post_author' => string '16' (length=2)
  public 'post_date' => string '2017-10-10 17:24:03' (length=19)
  public 'post_date_gmt' => string '0000-00-00 00:00:00' (length=19)

In my OR query, how can I target the post_date_gmt? I've tried compare == null, value = '' and I still can't seem to target it.

Here is the full code:

$args = array(
    'post_type'      => 'office',
    'post_status'    => 'any',
    'posts_per_page' => -1,
    'date_query' => array(
        array(
            'column' => 'post_modified_gmt',
            'before' => $started,
        ),
    ),
    'meta_query' => array(
        array(
            'relation' => 'OR',
            array(
                'key' => '_setting_update',
                'value' => '1',
                'compare' => '=='
            ),
            array(
                'key' => 'post_date_gmt',
                'value' => '',
                'compare' => '==',
            ),
        ),
    ),
);
$the_query = new WP_Query($args);

So I'm trying to target some posts to include in my function but I just can't seem to get it to work.

When I have the meta_query taken out I get 9 results (Works):

$args = array(
    'post_type'      => 'office',
    'post_status'    => 'any',
    'posts_per_page' => -1,
    'date_query' => array(
        array(
            'column' => 'post_modified_gmt',
            'before' => $started,
        ),
    ),
);
$the_query = new WP_Query($args);

Then I get one result as below:

object(WP_Post)[1306]
  public 'ID' => int 50794
  public 'post_author' => string '16' (length=2)
  public 'post_date' => string '2017-10-10 17:24:03' (length=19)
  public 'post_date_gmt' => string '0000-00-00 00:00:00' (length=19)

In my OR query, how can I target the post_date_gmt? I've tried compare == null, value = '' and I still can't seem to target it.

Here is the full code:

$args = array(
    'post_type'      => 'office',
    'post_status'    => 'any',
    'posts_per_page' => -1,
    'date_query' => array(
        array(
            'column' => 'post_modified_gmt',
            'before' => $started,
        ),
    ),
    'meta_query' => array(
        array(
            'relation' => 'OR',
            array(
                'key' => '_setting_update',
                'value' => '1',
                'compare' => '=='
            ),
            array(
                'key' => 'post_date_gmt',
                'value' => '',
                'compare' => '==',
            ),
        ),
    ),
);
$the_query = new WP_Query($args);
Share Improve this question asked Nov 8, 2019 at 17:11 DevSemDevSem 2092 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I've figured it out:

'meta_query' => array(
    array(
        'relation' => 'OR',
        array(
            'key' => '_setting_update',
            'value' => '1',
            'compare' => '=='
        ),
        array(
            'relation' => 'AND',
            array(
                'key' => '_created_date',
                'value' => '',
                'compare' => 'NOT EXISTS',
            ),
            array(
                'key' => '_modified_date',
                'value' => '',
                'compare' => 'NOT EXISTS',
            ),
        )
    ),
),

本文标签: phpTarget postdategmt if it39s empty or not set