Skip to content

Commit 291122b

Browse files
Check if post exists
1 parent 80cacd2 commit 291122b

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

features/post.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ Feature: Manage WordPress posts
264264
When I run `wp post url-to-id {POST_URL}`
265265
Then STDOUT should contain:
266266
"""
267-
{POST_ID}
267+
1
268268
"""
269269

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

276276
Scenario: Update a post from file or STDIN

src/Post_Command.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,17 @@ public function generate( $args, $assoc_args ) {
889889
* ## EXAMPLES
890890
*
891891
* # Get post ID by URL
892-
* $ wp post url-to-id post-url
892+
* $ wp post url-to-id https://example.com/?p=1
893893
*
894894
* @subcommand url-to-id
895895
*/
896896
public function url_to_id( $args, $assoc_args ) {
897897
$post_id = url_to_postid( $args[0] );
898898

899-
if ( ! $post_id ) {
900-
WP_CLI::error( "Could not get post with url '$args[0]'." );
899+
$post = get_post( $post_id );
900+
901+
if ( null === $post ) {
902+
WP_CLI::error( "Could not get post with url $args[0]." );
901903
}
902904

903905
WP_CLI::print_value( $post_id, $assoc_args );

0 commit comments

Comments
 (0)