Search by post ID

1

I am creating a system in WP that I am showing the post ID next to the title, but when I do the search, it is not searching for the ID, which does not find any posts ... Does anyone know how to do for Wordpress search by ID too and not just by title?

 <div class="container">

    <?php if ( have_posts()) : ?>

    <span class="tl-search">
        <?php _e( 'Resultado de busca por: ', 'abc' ); ?><h1 class="tlt-prod"><?php the_search_query(); ?></h1>
    </span>

    <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>
    <!-- <div id="nav-below" class="navigation">
        <div class="nav-previous">
            <?php next_posts_link(__( 'Próximo <span class="meta-nav">»</span>', 'contmatic')) ?></div>
        <div class="nav-next">
            <?php previous_posts_link(__( '<span class="meta-nav">«</span> Anterior', 'contmatic'  )) ?></div>
    </div> -->
    <!– #nav-above –>
    <?php } ?>

    <?php while ( have_posts() ) : the_post() ?>

    <div class="busca_search" id="post-<?php the_ID(); ?>">
        <a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'abc'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
            <h2 class="entry-title">
                <span class="id-post"> <?php the_ID(); ?> </span>
                <span class="tl-post"> <?php the_title(); ?> </span>
            </h2>

            <?php if ( $post-> $ID == 'post' ) { ?>


            <?php } ?>

            <div class="entry-summary">
                <?php the_excerpt( __( 'Continue lendo <span class="meta-nav">»</span>', 'contmatic' )  ); ?>
                <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'contmatic' ) . '&after=</div>') ?>
            </div>
            <!– .entry-summary –>

            <?php if ( $post->post_type == 'post' ) { ?>

            <!– #entry-utility –>
            <?php } ?>
        </a>
    </div>
     <!-- <?php the_ID(); ?> -->

    <?php endwhile; ?>


    <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>
    <div id="nav-below" class="navigation">
        <div class="nav-next">
            <?php previous_posts_link(__( '<span class="meta-nav">«</span> Anterior', 'contmatic'  )) ?></div>
        <div class="nav-previous">
            <?php next_posts_link(__( 'Próximo <span class="meta-nav">»</span>', 'contmatic')) ?></div>
    </div>
    <!– #nav-below –>
    <?php } ?>

    <?php else : ?>

    <div id="post-0" class="post no-results not-found">
        <span class="tl-search">
        <h1 class="tlt-prod">
            <?php _e( 'Nada Encontrado', 'abc' ) ?>
        </h1>
        </span>
        <div class="entry-content">
            <p>
                <?php _e( 'Desculpe, mas não encontramos nenhuma palavra com seu termo de busca. Por favor tente novamente.', 'abc' ); ?>
            </p>
        </div>
        <!– .entry-content –>
    </div>

    <?php endif; ?>


</div>

Search is like this

<div class="conteudo-pesquisa">
                    <form role="search" method="get" class="formulario-pesquisa" action="<?php 
                    echo home_url( '/' ); ?>">

                        <input type="search" class="input-pesquisa" placeholder="" value="<?php 
                        echo get_search_query() ?>" name="s"  required />

                    </form>
                </div>
    
asked by anonymous 27.03.2018 / 22:58

1 answer

1
To change the mode with WordPress works with queries , you need to use actions to do this.

These actions acts like a trigger that will be activated in a certain place by a certain code.

To modify the search or which posts will be displayed, you can use action pre_get_posts , for example:

<?php

/* Adiciona a "action" */
add_action( 'pre_get_posts', 'searchById' );

/**
 * Adiciona filtro através do ID
 *
 * @param WP_Query $query
 */
function searchById($query) {

    /* Captura o valor do parâmetro "S" da URL */
    $id = filter_input(INPUT_GET, 's');

    /**
     * Verifica se o usuário está "fora" do painel de admin
     * Verifica se é uma busca
     * Verifica se o valor informado é um valor numérico
     */
    if (!is_admin() && $query->is_search && is_numeric($id)) {

        /* Remove o parâmetro de busca da URL */
        $query->set('s', '');

        /* Filtra apenas as postagens com o ID passado */
        $query->set('post__in', [ (int)$id ]);
    }

}
    
28.03.2018 / 16:31