Search for posts based on their url in the admin?

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?

Topic posts Wordpress search

Category Web


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


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.


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.