Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
/ wp-cli-getbyurl Public archive

Get id by URL in WordPress for post types, taxonomies and media

License

Notifications You must be signed in to change notification settings

CodeAtCode/wp-cli-getbyurl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

How to use it

License

Warning

There is an official parameter for this in WP-CLI: https://developer.wordpress.org/cli/commands/post/url-to-id/

This package will be not updated!

Install

wp package install codeatcode/wp-cli-getbyurl

Output structure

Kind of content | post id | post type/taxonomy

Commands

To get the id and post type by url:

wp get-by-url --skip-plugins --skip-themes http://demo.test/2018/10/your-post

Output:

post | 33 | post

To get the id and taxonomy by url:

wp get-by-url --skip-plugins --skip-themes http://demo.test/tag/test/

Output:

term | 2 | post_tag

Works also with attachment.

Why skip plugins and themes

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.

Why the separator is pipe?

In that way is more easy to do script on it in bash or other languages

How to use it


# How to use the script:
#     cat list.txt | xargs -n1 remove-page-by-url.sh

out=$(wp get-by-url "$1" --skip-plugins --skip-themes)
if [[ -n $out ]]; then
    command=$(cut -d'|' -f1 <<< "$out" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//')
    id=$(cut -d'|' -f2 <<< "$out" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//')
    what=$(cut -d'|' -f3 <<< "$out" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//')
    if [[ "$what" == 'post' || "$what" == 'attachment' ]]; then
        wp "$command" delete "$id" --skip-plugins --skip-themes
    else
        wp "$command" delete "$what" "$id" --skip-plugins --skip-themes
    fi
fi