1

In the "posts -> all posts" admin page, I would like to search and see only posts with some element in their url (for example, the character "-3" to indicate they are a duplicate post).

How can this be done?

3 Answers 3

3

Here's one way to support searching for post slugs in the backend.

Let's invoke this kind of search by the slug: string in the search term.

Example

To search for slugs that end in -2 we want to be able to search for:

slug:*-2

where * is the wildcard.

Demo Plugin

Here's a demo plugin that might need further testing and adjustments:

add_filter( 'posts_search', function( $search, \WP_Query $q ) use ( &$wpdb )
{
    // Nothing to do
    if(    
           ! did_action( 'load-edit.php' ) 
        || ! is_admin() 
        || ! $q->is_search() 
        || ! $q->is_main_query() 
    )
        return $search;

    // Get the search input
    $s = $q->get( 's' );

    // Check for "slug:" part in the search input
    if( 'slug:' === mb_substr( trim( $s ), 0, 5 ) )
    {
        // Override the search query 
        $search = $wpdb->prepare(
            " AND {$wpdb->posts}.post_name LIKE %s ",
            str_replace( 
                [ '**', '*' ], 
                [ '*',  '%' ],  
                mb_strtolower( 
                    $wpdb->esc_like( 
                        trim( mb_substr( $s, 5 ) ) 
                    ) 
                )
            )
        );

        // Adjust the ordering
        $q->set('orderby', 'post_name' );
        $q->set('order', 'ASC' );
    }
    return $search;
}, PHP_INT_MAX, 2 );

This is based on the _name__like plugin in my previous answer here.

1

You can't really perform that kind of query from the admin UI without explicitly coding it in a theme or plugin. If you're looking to find all posts ending with -3 in their name, I suggest running an SQL query, perhaps couple it with some WP-CLI magic too:

wp post list --fields=url --post__in=$(wp db query "SELECT ID FROM wp_posts WHERE post_name LIKE '%-3';" | paste -s -d,)

Hope that helps.

0

Or you can go to your mysql database ,Use the search option to find for wp_posts posts table in the column post_name

And perform a search!

Screenshot :https://prnt.sc/SDQ3QZL1kyZ1

😊

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.