wordpress group by post type on search page -
i display search results grouped post type. have regular posts, pages, , custom post type of product. how accomplish editing below code. code below shows posts , pages right now.
<?php while (have_posts()) : the_post(); echo "<h1>"; echo $post->post_type; echo $post->post_title; echo "</h1>"; endwhile; ?>
this code alters original search query order results post-type in order select. there other solutions, 1 found doesn't break pagination or requires multiple queries.
add_filter('posts_orderby', 'my_custom_orderby', 10, 2); function my_custom_orderby($orderby_statement, $object) { global $wpdb; if (!is_search()) return $orderby_statement; // disable filter future queries (only use filter main query in search page) remove_filter(current_filter(), __function__); $orderby_statement = "field(".$wpdb - > prefix. "posts.post_type, 'post-type-c', 'post-type-example-a', 'custom-post-type-b') asc"; return $orderby_statement; }
Comments
Post a Comment