web-dev-qa-db-fra.com

Comment rechercher un article par ID dans wp-admin

Lorsque je vais à une liste de messages dans la zone d'administration, je ne peux effectuer une recherche que par titre ou contenu.

Je souhaite ajouter une fonction qui effectue une recherche par courrier ou par identifiant de page, ce qui permet aux éditeurs de trouver plus facilement le message qu'ils recherchent.

Je dois l'implémenter dans le dossier du plugin. Des idées? Je ne sais pas par où je devrais commencer maintenant. Merci les gars!

 enter image description here 

2
Nobody

Solution 1

Voici une solution qui utilise l'action pre_get_posts pour remplacer la requête de recherche d'origine et rechercher par ID de publication à la place. C'est l'approche que je recommanderais généralement.

/**
 * Allows posts to be searched by ID in the admin area.
 * 
 * @param WP_Query $query The WP_Query instance (passed by reference).
 */
add_action( 'pre_get_posts','wpse_admin_search_include_ids' );
function wpse_admin_search_include_ids( $query ) {
    // Bail if we are not in the admin area
    if ( ! is_admin() ) {
        return;
    }

    // Bail if this is not the search query.
    if ( ! $query->is_main_query() && ! $query->is_search() ) {
        return;
    }   

    // Get the value that is being searched.
    $search_string = get_query_var( 's' );

    // Bail if the search string is not an integer.
    if ( ! filter_var( $search_string, FILTER_VALIDATE_INT ) ) {
        return;
    }

    // Set WP Query's p value to the searched post ID.
    $query->set( 'p', intval( $search_string ) );

    // Reset the search value to prevent standard search from being used.
    $query->set( 's', '' );
}

Solution 2

Voici une solution alternative qui utilise le filtre posts_search pour modifier le code SQL directement lorsqu'une recherche est effectuée dans la zone d'administration à l'aide d'une valeur numérique.

/**
 * Modify search SQL enabling searching by post ID.
 *
 * @param string   $search Search SQL for WHERE clause.
 * @param WP_Query $wp_query   The current WP_Query object.
 */
add_filter( 'posts_search', 'wpse_posts_search_post_id', 10, 2 );
function wpse_posts_search_post_id( $search, $wp_query ) {
    // Bail if we are not in the admin area
    if ( ! is_admin() ) {
        return $search;
    }

    // Bail if this is not the search query.
    if ( ! $wp_query->is_main_query() && ! $wp_query->is_search() ) {
        return $search;
    }   

    // Get the value that is being searched.
    $search_string = get_query_var( 's' );

    // Bail if the search string is not an integer.
    if ( ! filter_var( $search_string, FILTER_VALIDATE_INT ) ) {
        return $search;
    }

    // This appears to be a search using a post ID.
    // Return modified posts_search clause.
    return "AND wp_posts.ID = '" . intval( $search_string )  . "'";
}

En utilisant ce code

Chacune de ces solutions peut être facilement transformée en un plugin en enregistrant l'extrait de code dans un fichier nommé admin-search-by-post-id.php dans un dossier nommé admin-search-by-post-id (par exemple). Voir le Manuel du développeur sur la création de plugins pour plus de détails.

Exemple d'en-tête de plugin:

<?php
/*
Plugin Name:  Admin Search by Post ID
Plugin URI:   
Description:  Allows posts to be searched using IDs in the admin area.
Version:      0.0.1
Author:       
Author URI:   
License:      GPL2
License URI:  https://www.gnu.org/licenses/gpl-2.0.html
Text Domain:  your_text_domain
Domain Path:  /languages
*/
3
Dave Romsey