Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Search results count

  • Hello,

    Is it possible to display the search results count? For example -> 970 search results. If so, How?

  • Hi there,

    For reference, could you share the link to your site? I’ll check how your Search page is setup and see what code is needed.

  • View below

  • Try adding this snippet:

    function custom_search_result_title( $items) { 
       if ( is_search() ) { 
    	  global $wp_query;
          $found_posts = $wp_query->found_posts;
          $items = $items . '<h3 class="found-posts-count">' . $found_posts . ' Results</h3>';
       } 
       return $items; 
    } 
    
    add_filter( 'generate_search_title_output', 'custom_search_result_title', 9999, 2 ); 

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

  • Is it possible to make a shortcode of this snippet or can you explain how I add this in front of the search results page title? The title is now “Zoekresultaten voor: … ” but I want this to be “23 zoekresultaten voor: … “.

  • Use function below to create shortcode [custom_search_result_title]:

    function custom_search_result_title_shortcode( $atts ) {
       ob_start(); 
    
       if ( is_search() ) {
          global $wp_query;
          $found_posts = $wp_query->found_posts;
          echo '<h3 class="found-posts-count">' . $found_posts . ' Results</h3>';
       }
    
       $output = ob_get_clean(); 
       return $output;
    }
    add_shortcode( 'custom_search_result_title', 'custom_search_result_title_shortcode' );
  • Thank you, and where can I add this? Because it looks like the title on my search results pages isn’t from my custom content block but maybe from an theme template file?

  • Thank you, and where can I add this

    What do you mean?

  • View screenshot 1 displays the current appearance of the search results page title, while screenshot 2 illustrates what I want to achieve.

  • You don’t want it to show the 3?

  • I want the count of the search results to be displayed in the page title of the search results page.

  • I see. It won’t be a Shortcode though. Try this code instead:

    function custom_search_result_title( $items) { 
       if ( is_search() ) { 
    	  global $wp_query;
          $found_posts = $wp_query->found_posts;
    	  $myreplace = '</h1>';
          $myinsert = '<span class="found-posts-count"> ' . $found_posts . ' Results</span></h1>';
    	  $items = str_replace( $myreplace, $myinsert , $items );
       } 
       return $items; 
    } 
    
    add_filter( 'generate_search_title_output', 'custom_search_result_title', 9999, 2 ); 
  • Works, but that snippet isn’t adding the count inside the page title.

  • Could you reactivate the code? I’ll check what’s going on.

  • Okay, I did now.

  • I see. I updated the code I just shared above. Can you try that instead?

Viewing 16 posts - 1 through 16 (of 20 total)
  • You must be logged in to reply to this topic.