0

I am building a company directory for a website with business news. Now what I would like is to add pages of companies with some common information and a list of published posts about that company. So what you see if you type https://website.com/?s=starbucks. Does anybody knows a solution for this? Thanks a lot! Kind regards, Vincent

1 Answer 1

0

Please refer to the updated comment.


You have to do some changes to the search functionality by default.

First, if you want to use different templates for each kind of search, you can add conditions in the search.php file.

if(isset($_GET['search-type'])) {
   $type = $_GET['search-type'];
   if ($type == 'global') {
      load_template(TEMPLATEPATH . '/search-global.php');
   } elseif ($type == 'blog') {
      load_template(TEMPLATEPATH . '/search-blog.php');
   }
}

You want to add the field search-type in your search form. This fields will be hidden, because only is need it to separate the kind of search, in the case you will implement several different searches.

<form id="searchform-global" method="get" action="<?php echo home_url('/search/'); ?>" role="global-search">
      <input id="s" type="search" name="s" placeholder="Search global...">
      <input type="hidden" name="search-type" value="global" />
      <button id="searchsubmit" type="submit">Search</button>
</form>

Now, in the code above you can see that the action is pointing to /search/ path. It is important to use the action pre_get_posts, in order to tell to WordPress that the page search is a search results page. Obviously, you have to create the page first in WordPress dashboard.

So, you have to use the below code in your functions.php.

function new_search_global( $query ) {

   $page_id = 43; // This is ID of page with your structure -> http://example.com/mysearch/
   $per_page = -1; // Get all posts.

   // Now we must edit only query on this one page
   if ( !is_admin() && $query->is_main_query() && is_page($page_id) ) {
      // Also, if you want to add a specific class for your template search
      add_filter( 'body_class', function( $classes ) {
            $classes[] = 'global-search';
            return $classes;
      } );
      $query->set( 'pagename', '' ); // we reset this one to empty!
      $query->set( 'posts_per_page', $per_page ); // Set posts per page.
      $query->is_search = true; // We making WP think it is Search page 
      $query->is_page = false; // disable unnecessary WP condition
      $query->is_singular = false; // disable unnecessary WP condition
   }
}
add_action( 'pre_get_posts', 'new_search_global' );

If you created search-global.php new template for your search, there you can create a new query, in order to get the search and other stuff. In the below example, I'm using $search to get the search query, and retrieving all posts of that search:

$search = get_search_query();

// WP_Query arguments
$args = array(
    'post_type' => 'post',
    'posts_per_page' => '-1',
    's' => $search
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
            // do stuff here
           
    }
} else {
    // no posts found
}

 // Restore original Post Data
 wp_reset_postdata();
     

Finally, if you do your search, you will get the following URL: https://yoururl.com/search/?s=yoursearch&search-type=global.

Additionally, and just in case, you want to refresh your permalinks structure going to Dashboard > Settings > Permalinks > Save settings.

Hope to help you! :)


UPDATE

I just figured out that maybe I misunderstood your question (I will leave above information just for reference). If you want to only show a specific search on a page, you only need to create a new query there, like the following:

// WP_Query arguments
$args = array(
    'post_type' => 'post',
    'posts_per_page' => '-1',
    's' => 'starbucks'
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
            // do stuff here
           
    }
} else {
    // no posts found
}

 // Restore original Post Data
 wp_reset_postdata();
     

The 's' parameter is making the search starbucks. In the args you can add more conditions to retrieve posts. You can read further in the codex about WP_Query here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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