Ответы пользователя по тегу WordPress
  • Wordpress: поиск по id поста?

    @ivaros
    В моем случае помогла комбинациия действий:
    в functions.php
    function my_search_pre_get_posts($query)
    {
    
        // If it's not a search return
        if( !isset( $query->query_vars['p'] ) )
            return;
    
        // If it's a search but there's no prefix, return
        if( '#' != substr( $query->query_vars['p'], 0, 1 ) )
            return;
    
        // Validate the numeric value
        $id = absint( substr( $query->query_vars['p'], 1 ) );
        if( !$id )
            return; // Return if no ID, absint returns 0 for invalid values
    
        // If we reach here, all criteria is fulfilled, unset search and select by ID instead
        unset( $query->query_vars['p'] );
        $query->query_vars['p'] = $id;
    }
    
    // Filter the search page
    add_filter('pre_get_posts', 'my_search_pre_get_posts');


    2) На странице с поиском вставляем
    <?php 
    add_filter('pre_get_posts', 'my_search_pre_get_posts');
     ?>
    
    <div class="search-block">
        <form role="search" method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
            <input id="s" type="text" placeholder="введите номер проекта" name="s" value="">
            <input type="submit" id="searchsubmit" value="Найти проект" />
        </form>
    </div>


    Надеюсь мой комент будет Вам полезен :)))
    Ответ написан