Skip to content

Commit

Permalink
Check if post exists
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianBustamante committed Nov 17, 2023
1 parent 80cacd2 commit 291122b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 4 additions & 4 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ Feature: Manage WordPress posts
When I run `wp post url-to-id {POST_URL}`
Then STDOUT should contain:
"""
{POST_ID}
1
"""

When I run `wp post url-to-id non-existent-url`
Then STDOUT should contain:
When I try `wp post url-to-id 'https://example.com/?p=404'`
Then STDERR should contain:
"""
Could not get post with url 'non-existent-url'.
Could not get post with url https://example.com/?p=404.
"""

Scenario: Update a post from file or STDIN
Expand Down
8 changes: 5 additions & 3 deletions src/Post_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,15 +889,17 @@ public function generate( $args, $assoc_args ) {
* ## EXAMPLES
*
* # Get post ID by URL
* $ wp post url-to-id post-url
* $ wp post url-to-id https://example.com/?p=1
*
* @subcommand url-to-id
*/
public function url_to_id( $args, $assoc_args ) {
$post_id = url_to_postid( $args[0] );

if ( ! $post_id ) {
WP_CLI::error( "Could not get post with url '$args[0]'." );
$post = get_post( $post_id );

if ( null === $post ) {
WP_CLI::error( "Could not get post with url $args[0]." );
}

WP_CLI::print_value( $post_id, $assoc_args );
Expand Down

0 comments on commit 291122b

Please sign in to comment.