Skip to content

wp post list --date_query before array #3039

Closed

Description

@grappler

I would like to query all post before 2015. I was able to query all posts from 2015 using wp post list --post_type=post --format=ids --date_query=2015

How can I make this query using wp-cli?

'date_query' => array(
    'before' => array(
        'year' => '2015'
    ),
),

https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

Activity

  1. ernilambar commented on Jun 20, 2016

    @ernilambar
    Member

    To fetch all posts from 2015, command should be similar to
    wp post list --post_type=post --format=ids --year=2015

    Currently query parameters are directly merged with default and passed to the WP_Query call. https://github.com/wp-cli/wp-cli/blob/master/php/commands/post.php#L351 I doubt such type of query is currently possible. May be we can introduce some kind of mechanism where list command accepts multi level query parameters.

    wp post list --post_type=post --format=ids --date_query='{"before":{"year":"2015"}}'
    Just one noob idea :D

    Then we can support meta_query and tax_query in similar way.

    Or will this be too much for a single command?

  2. danielbachhuber commented on Jun 20, 2016

    @danielbachhuber
    Member

    I doubt such type of query is currently possible. May be we can introduce some kind of mechanism where list command accepts multi level query parameters.

    I've considered this in the past, but never identified a syntax I thought worked well.

    Or will this be too much for a single command?

    The future of wp post list is wp rest post list, so I don't think we need to solve the problem here https://github.com/danielbachhuber/wp-rest-cli

  3. grappler commented on Jun 20, 2016

    @grappler
    Author

    @danielbachhuber So what I would like to do is not possible with wp-cli but is possible with wp-rest-cli?

  4. danielbachhuber commented on Jun 20, 2016

    @danielbachhuber
    Member

    So what I would like to do is not possible with wp-cli but is possible with wp-rest-cli?

    Yes:

    wp rest post list --after="2014-12-31T23:59:59" --before="2016-01-01T00:00:00"
    
  5. tmuka commented on Feb 14, 2019

    @tmuka

    So what I would like to do is not possible with wp-cli but is possible with wp-rest-cli?

    Yes:

    wp rest post list --after="2014-12-31T23:59:59" --before="2016-01-01T00:00:00"
    

    just to save other people seeing this solution time, wp rest post list is only capable of returning 100 post results at a time... it was more efficient for me to run wp post list --year queries for each year's worth of posts i needed to modify.

  6. formido commented on Feb 4, 2021

    @formido

    So what I would like to do is not possible with wp-cli but is possible with wp-rest-cli?

    Yes:

    wp rest post list --after="2014-12-31T23:59:59" --before="2016-01-01T00:00:00"
    

    just to save other people seeing this solution time, wp rest post list is only capable of returning 100 post results at a time... it was more efficient for me to run wp post list --year queries for each year's worth of posts i needed to modify.

    Pagination makes sense for a network api but for a cli that passes off a ton of complexity onto the user.

  7. davidsword commented on Nov 29, 2023

    @davidsword

    I used this hacky snippet to get the --date_query='[{"after":"7 day ago"}]' to work in wp cli for a site I needed it on:

    add_action('pre_get_posts', function( $query ){
    	if ( ! WP_CLI ) 
    		return;
    	
    	if (isset($query->query_vars['date_query'])) {
    		if ( ! empty( $query->query_vars['date_query'] ) && ! is_array( $query->query_vars['date_query'] ) ) {
    			$is_json = json_decode($query->query_vars['date_query'], true);
    			if (is_array($is_json))
    				$query->query_vars['date_query'] = $is_json;
    		}
    	}
    });```
  8. skorasaurus commented on Nov 22, 2024

    @skorasaurus

    Since it's been 6 years and there hasn't been active development of wp-rest-cli in the last couple years, so is wp-cli still the more appropriate place for this to be solved?

    If so, looks like this is blocked by #4616

  9. skorasaurus commented on Feb 18, 2025

    @skorasaurus

    appears that this is now implemented! wp-cli/entity-command#520

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions