5

I want to use the search box for searching post by Post ID and display the content of the post in the search result. I have gone for a deep search but didn't get any satisfactory answer. I dont need the URL of the post, I need all the content of the post or page.

Please help.

| improve this question | | | | |
4

Put any of the filters in the functions.php

Using pre get posts

// Filter the search page
add_filter('pre_get_posts', 'search_pre_get_posts');

function search_pre_get_posts($query)
{
    // Verify that we are on the search page that that this came from the event search form
    if($query->query_vars['s'] != '' && is_search())
    {
        // If "s" is a positive integer, assume post id search and change the search variables
        if(absint($query->query_vars['s']))
        {
            // Set the post id value
            $query->set('p', $query->query_vars['s']);

            // Reset the search value
            //$query->set('s', '');
        }
    }
}

Using posts where query

add_filter('posts_where', 'posts_where');
    function posts_where($where)
        {

            $s = $_GET['s'];

            if(!empty($s))
            {
              if(is_numeric($s))
              {
                global $wpdb;

                $where = str_replace('(' . $wpdb->posts . '.post_title LIKE', '(' . $wpdb->posts . '.ID = ' . $s . ') OR (' . $wpdb->posts . '.post_title LIKE', $where);
              }
              elseif(preg_match("/^(\d+)(,\s*\d+)*\$/", $s)) // string of post IDs
              {
                global $wpdb;

                $where = str_replace('(' . $wpdb->posts . '.post_title LIKE', '(' . $wpdb->posts . '.ID in (' . $s . ')) OR (' . $wpdb->posts . '.post_title LIKE', $where);
              }
            }


          return $where;
        }

Use any one functions as your need. The first function gives you exact search using search id. ex: 1 gives you 1 id's post only.

Now how to show posts all value I am given you hints.

Edit your search.php or add search.php in your theme. Basically every theme has search.php, so you just need to modify a little bit.

I am giving you some example code for search.php. you will modify it as your need.

<?php
if ( have_posts() ) :
while ( have_posts() ) :  the_post();
$post_id = get_the_ID();
$id = get_post_thumbnail_id($post_id);
$image = wp_get_attachment_image_url($id,'full');
?>
// Print The title
<?php echo get_the_title($post_id); ?>
//print full content of the post
<?php echo get_the_content(); ?>
//print the excert
<?php print get_the_excerpt($post_id); ?>
// print the image  
<img src="<?php echo $image; ?>" class="attachment-portfolio-three size-portfolio-three wp-post-image" alt="">
// suppose custom field
<a href="<?php echo get_the_permalink($post_id); ?>">
                          <?php echo get_post_meta($post_id,'custom_field_1',true);?>
                        </a>
// suppose custom field 2
<a href="<?php echo get_the_permalink($post_id); ?>">
                          <?php echo get_post_meta($post_id,'custom_field_2',true);?>
                        </a>
// suppose custom field  3                          
<a href="<?php echo get_the_permalink($post_id); ?>">
                          <?php echo get_post_meta($post_id,'custom_field_2',true);?>
                        </a>                                                        
<?php endwhile;?>
<?php wp_reset_query();
// print pagination and also its arg if you use my code.
paginate_links($args); 
else :
?>
<p><?php _e( 'Sorry, but nothing matched your search terms. Please try again with some different keywords.', 'twentyseventeen' ); ?></p>
  <?php
    get_search_form();

endif;
?>
| improve this answer | | | | |
  • Using pre_get_posts filter is better & clean. +1 for detailed answer. – Fayaz Apr 1 '17 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.