Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 5905d7b

Browse files
committed
feat(media): get id by attachment url
1 parent 9181335 commit 5905d7b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

command.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,27 @@
1010
* ## OPTIONS
1111
*
1212
* <url>
13-
* : Number of users to generate
1413
*
1514
* ## EXAMPLES
1615
*
1716
* wp get-by-url [your-url-with-slug]
1817
*
1918
* @accesspublic
2019
* @param array $args
21-
* @param array $assoc_args
2220
* @return
2321
*/
2422
function codeat_get_by_url( $args ){
2523
$url = esc_html( $args[0] );
2624

25+
$hasext = pathinfo( $url, PATHINFO_EXTENSION );
26+
if ( !empty( $hasext ) ) {
27+
$media = attachment_url_to_postid( $url );
28+
if ( $media !== 0 ) {
29+
WP_CLI::log( 'media | ' . $media . ' | attachment' );
30+
return;
31+
}
32+
}
33+
2734
$post = url_to_post( $url );
2835
if ( is_object( $post ) ) {
2936
WP_CLI::log( 'post | ' . $post->ID . ' | ' . $post->post_type );

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Output:
2424

2525
`term | 2 | post_tag`
2626

27+
Works also with attachment.
28+
2729
### Why skip plugins and themes
2830

2931
It is a good practice in WP CLI when you have to execute commands that are heavy of resources to not load plugins and themes for a lot of reasons.

0 commit comments

Comments
 (0)