Search for the post ID

Asked

Viewed 154 times

1

I am creating a WP system that I am showing the ID of the post next to the title, but when I do the search, is not searching for the ID, da que não encontra nenhum post... Someone knows how to do for Wordpress search by ID also and not only 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>

Research 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>
  • How’s that search going?

1 answer

1


To change the mode with Wordpress works with the darlings, it is necessary to use actions for that reason.

Those actions acts as a trigger that will be activated in a certain place, by a certain code.

To modify the search or what 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 ]);
    }

}
  • but where do I put this pre_get_posts? sorry to be unaware...

  • @Rafaelcastro just add this code to your functions.php

  • thank you very much, it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.