Skip to content

Add url-to-id commands for each entity #429

@iandunn

Description

@iandunn

Feature Request

It'd be convenient to be able to wp post get foo instead of having to manually look up the ID and then run wp post get 32425.

Is your feature request directly related to a specific, existing command?

The user interacts with post get in the entities repo, but it seems like it'd be implemented in Fetchers\Post in this repo.

Describe your use case and the problem you are facing

I often know a URL like https://example.org/my-cpt/foo/, and want to run something like wp post get 32523 to get some info about the post. In order to do that, I have to:

  1. Switch to a browser
  2. Login to wp-admin
  3. Visit the URL
  4. Click Edit in the admin toolbar
  5. Copy the ID out of the URL
  6. Switch back to terminal
  7. Paste the ID

If feels like that could be simplified.

Describe the solution you'd like

Fetchers\User, term get, etc already allow passing slugs in addition to IDs. It'd be much more convenient if I just could type wp post get my-cpt/foo. Maybe something like:

if ( is_numeric( $identifier ) ) {
    $post = get_post( $identifier );
} else {
    $cpt_slug = 'my_cpt'; // get by extracting first part of path and mapping to a rewrite rule?
    $post     = get_page_by_path( 'foo', OBJECT, $cpt_slug );
}

There'd need to be extra logic to handle other URL patterns like archives etc, but there may be a way to leverage parse_request() or something to do most of that work for us.

Alternatively, maybe I could pass a full --URL, and then leave ID empty, like:

wp post get --url=https://example.org/my-cpt/foo

...and the bootstrap process would populate the global $post based on that?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions