Skip to content

Commit bb9bb6c

Browse files
Add command to get the post ID by URL
1 parent ce37f3c commit bb9bb6c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

features/post.feature

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ Feature: Manage WordPress posts
259259
"""
260260
https://example.com/?p=1
261261
"""
262+
And save STDOUT as {POST_URL}
263+
264+
When I run `wp post url-to-id {POST_URL}`
265+
Then STDOUT should contain:
266+
"""
267+
{POST_ID}
268+
"""
262269

263270
Scenario: Update a post from file or STDIN
264271
Given a content.html file:

src/Post_Command.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,31 @@ public function generate( $args, $assoc_args ) {
878878
}
879879
}
880880

881+
/**
882+
* Gets post ID by URL.
883+
*
884+
* ## OPTIONS
885+
*
886+
* <url>
887+
* : The URL of the post to get.
888+
*
889+
* ## EXAMPLES
890+
*
891+
* # Get post ID by URL
892+
* $ wp post url-to-id post-url
893+
*
894+
* @subcommand url-to-id
895+
*/
896+
public function url_to_id( $args, $assoc_args ) {
897+
$post_id = url_to_postid( $args[0] );
898+
899+
if ( ! $value ) {
900+
WP_CLI::error( "Could not get post with url '{$args[0]}'." );
901+
}
902+
903+
WP_CLI::print_value( $post_id, $assoc_args );
904+
}
905+
881906
private function maybe_make_child() {
882907
// 50% chance of making child post.
883908
return ( wp_rand( 1, 2 ) === 1 );

0 commit comments

Comments
 (0)