Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

custom search results – order results differently by post type

The below should loop your post types and only insert the header once if it has a post for that post type. I am sure there is a lot better ways to code this cleaner but this should work * I have not tested only coded in this page.

I am not sure about the ordering of the custom post type attribute as I do not know that plugin and your query looks to be correct might be worth trying without the _ as I have done this in Advanced Custom Fields and did not require the _ .

would be worth checking your database as I know advanced custom fields saves dates like this 03212017 and WordPress might not be able to see this as a valid date.

  if (have_posts()) :


    $counter = 1;
    $post_counter_1 = 0;
    $post_counter_2 = 0;
    $post_counter_3 = 0;


    while ( have_posts() ) : the_post();

    // Get post type to display
    if(get_post_type() == 'post'){
        // run once for creating header
       if($post_counter_1 == 0)
       {
        $post_counter_1 = 1;
        echo '<h4><i class="fa fa-newspaper-o"></i> Articles</h4>';
       }
       echo '<div class="results post">';
       get_template_part('parts/search', 'post');

    }
endwhile;
while ( have_posts() ) : the_post();
    if(get_post_type() == 'tribe_events'){

       if($post_counter_2 == 0)
       {
         $post_counter_2 = 1;
         echo '<h4><i class="fa fa-truck"></i> Distributors Wanted</h4>';
       }
       echo '<div class="results tribe_events">';
       get_template_part('parts/search', 'tribe_events');
    }
endwhile;

while ( have_posts() ) : the_post();
    if(get_post_type() == 'distribute'){

       if($post_counter_3 == 0)
       {
         $post_counter_3 = 1;
         echo '<h4 class="events">Events</h4>';
       }
      echo '<div class="results distribute">';
      get_template_part('parts/search', 'distribute');
    }
endwhile;
endif;

Related Posts:

  1. Advanced search form with filters for custom taxonomies and custom fields
  2. Admin Area Custom Type Search By Meta Fields Without Title & Content
  3. How do I Paginate Search Results for Custom Post Types?
  4. Search everything (posts, pages, tags, cpt, meta)
  5. How Do I Use WP_Query to Run This Database Query as Search Result?
  6. WordPress Admin Panel search posts with custom post meta values along with title
  7. How can I include meta box content when searching?
  8. Custom search for custom post meta with pre_get_posts interferes with WP search
  9. Search CPT Title AND Meta
  10. WordPress CPT Taxonomy Dashboard Search – How to include taxonomy in search?
  11. Advanced search form with filters for custom taxonomies and custom fields
  12. Show Custom Post Type meta boxes only on Page Edit
  13. How do i search authors from search form using author’s name
  14. Custom filter function not working with Custom post type
  15. Remotely search WordPress sites using xml-rpc
  16. BBPress Search results in WordPress search
  17. Limit Search to Post Type With Apostrophe return search not found
  18. Search filter triggered & sort by custom post type
  19. Set Custom Post Type in Widget Settings for use in Widget
  20. Displaying and searching Custom Posts
  21. Search by tag name and category
  22. Importing Data from a Non-WordPress database, into WP
  23. Getting thumbnails of a custom posts featured images in a page-templates metabox
  24. Searching Custom Fields
  25. Advanced search form with filters for custom taxonomies
  26. dropdowboxes with pre-selected custom fields optios that filters the results according seach needs
  27. filter search custom field query
  28. Different results between permalink and query var search
  29. List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
  30. Remove Post Custom Meta Box
  31. How to let users choose where to search for posts?
  32. WordPress Custom Search by post_type
  33. Include custom taxonomy term in search
  34. Can’t publish custom post type – “You are not allowed to edit this post.”
  35. Custom fields for custom post type
  36. Show future events based on custom field AND order by date
  37. Meta Data for Custom Post Type not saving
  38. Using preg_replace to filter custom textarea
  39. What content types are shown at a search page?
  40. Can NOT search my custom post type?
  41. Check if post with same meta value exists
  42. Filter date from post meta when date is in string format
  43. Search one custom post type ONLY, disable “all posts”
  44. Get only used meta_values
  45. If Post Meta Equals Current Title Conditional Advanced Custom Fields
  46. Is there a way to exclude posts based on meta_values?
  47. Custom post_type search pages
  48. Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
  49. Pagination Error : Duplicate argument being outputted
  50. Archieve.php not loading for custom post type
  51. WordPress search and date filter not working with custom post type
  52. Custom Empty Results page for my Custom Post Type
  53. Submitting Custom Post Types with custom fields from Front-end form
  54. create custom meta box with default value
  55. Get $post->ID from a URL
  56. Displaying a div from an assigned meta_value when on a page
  57. Meta data (Tags and Categories) for Custom Posts not showing.
  58. Display different information of a custom post type
  59. Create Array from custom post type to display a slider
  60. Setting Post Title via Meta Data returns Auto-draft
  61. How to Echo Metadata Value in Currency Format
  62. Issue on Working with Metabox – Checkbox to enable Sale Items
  63. Filter posts by tax (dropdown) and meta value
  64. delete_post_meta() for whole CPT / multiple posts?
  65. Custom filter for main search: how to exclude specific post_type from search results
  66. How to get specific post meta by title or id
  67. Publish and save specific postmeta to a filtered post_type
  68. Add custom attributes to a post
  69. How to move a post to different post type with all meta data?
  70. Why do I have to press the “Update” button twice to save my meta box values?
  71. Display an icon based on post type
  72. How can I display a drop-down select of Post Names
  73. how to show records that don’t have custom meta value
  74. how to get this tax_query working?
  75. Select2 AJAX and WP Query Returns ALL and does not filter
  76. Custom Taxonomy Search result page
  77. How to group search results by post type and only if post type result is not empty?
  78. WP API Response does not show my registered metadata
  79. How to detect that the save_post hook is calling the callback associated to the current edit post page only
  80. find custom post type post by searching its custom field with my string
  81. search custom post type by custom fields on front end
  82. $post->post_meta not pulling any post in wordpress/php
  83. Values from meta-box not on $_POST
  84. Prevent A Specific Custom Post Type Showing In WP Search Results Page
  85. Category Search / Custom Post Type search on my website. Custom Post Types that are “page-like”?
  86. WP_Query of custom post type sorted by meta_key has unexpected results
  87. Returning a custom content types with meta values
  88. Custom post type suddenly stoped working after WordPress 5 update
  89. Batch Extract Date from post title and put into ACF custom field
  90. copy images from custom field to another custom field
  91. Custom Search | check multiple meta_value for search value
  92. Hide custom post type from search based on custom taxonomy
  93. How to show custom field on specific custom post type posts only when filled
  94. How to handle this specific case of custom post type?
  95. What is the correct term for a tool that filters content based on metadata?
  96. Pagination on search results page
  97. Save CTP post meta different values each day without overwriting previous values
  98. get_post_meta returns NULL in front-end, but correct result in back-end
  99. Ideas on how to organize a project [closed]
  100. Cannot obtain custom meta information
Categories custom-post-types Tags custom-post-types, post-meta, search
Browser extension to allow users to submit posts front end
Disable custom image size creation for plugins

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress