Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp-cli subcommands on Windows Powershell #5679

Open
timotheemoulin opened this issue Sep 14, 2022 · 2 comments
Open

wp-cli subcommands on Windows Powershell #5679

timotheemoulin opened this issue Sep 14, 2022 · 2 comments

Comments

@timotheemoulin
Copy link

Looking at the official WP documentation here, the wp post delete command should be able to take a subcommand to list all the IDs to delete.

This works well on a Linux like system, but doesn't on Windows Powershell.

I know that it is more a Windows+Powershell issue, but that would be nice if we could find a workaround solution for this.

Expectations

# Delete all pages
$ wp post delete $(wp post list --post_type='page' --format=ids)
Success: Trashed post 1164.
Success: Trashed post 1186.

Reality

# Delete all pages
$ wp post delete $(wp post list --post_type='page' --format=ids)
Success: Trashed post 1164 1186.

In that case, post id 1186 has not been deleted because the subcommand result is interpreted as a single string parameter given to the delete command.
The \WP_CLI\CommandWithDBObject::_delete function that should receive $args as an array of IDs receive in reality one string param with the value 1164 1186 which evaluates as 1164 when casted to integer.

Function parameters type hint
Moreover, the wp_delete_post() function that is called underneath, should receive an integer value as first parameter.

/**
 * @param int  $postid       Optional. Post ID. Default 0.
 * @param bool $force_delete Optional. Whether to bypass Trash and force deletion.
 *                           Default false.
 * @return WP_Post|false|null Post data on success, false or null on failure.
 */
function wp_delete_post( $postid = 0, $force_delete = false ) {}
/**
 * Filters whether a post deletion should take place.
 *
 * @since 4.4.0
 *
 * @param WP_Post|false|null $delete       Whether to go forward with deletion. @TODO description
 * @param WP_Post            $post         Post object.
 * @param bool               $force_delete Whether to bypass the Trash.
 */
$check = apply_filters( 'pre_delete_post', null, $post, $force_delete );

I don't know if parameters hints will be enforced later, but there might be an issue there too as this function and the hooks triggered are all expecting an integer value.

@danielbachhuber danielbachhuber transferred this issue from wp-cli/entity-command Sep 27, 2022
@danielbachhuber
Copy link
Member

Thanks for the report, @timotheemoulin !

I moved this issue to wp-cli/wp-cli because I think it's related to how arguments are passed/parsed on Powershell. Here's some past work on PowerShell compatibility, if it's helpful: https://github.com/ericmann/WP-PowerShell

Can you share wp cli info ?

Also, what happens when you run wp post delete 1164 1186? Do you see the same error as wp post delete $(wp post list --post_type='page' --format=ids) ?

As a point of reference, here's where WP-CLI reads $GLOBALS['argv']:

$argv = array_slice( $GLOBALS['argv'], 1 );

@campusboy87
Copy link

campusboy87 commented Feb 7, 2024

I faced the same problem and put together such a solution:

cd G:\serverl6\www\wp-test.edit

# 1 option, fast, but you can't transfer a lot of ids
$ids = $(wp post list --post_type='guides' --format=ids --posts_per_page=1000)
$ids = $ids.Split(" ")
wp post delete $ids --force

<#
# Option 2, slow, but you can transfer an unlimited number of ids
$ids = $(wp post list --post_type='guides' --format=ids)
$numbers = $ids.Split(" ")

foreach ($id in $numbers) {
    wp post delete $id --force
}
#>

Read-Host -Prompt "Press Enter to exit"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants