Search only topics not forum / replies
-
Hi,
I’d like to have a search box that only searches topics but forum & replies. What file or hooks should I change? Please enlighten me 🙁 Thanks.
-
Hmph… okay… I’ve noticed that /plugins/bbpress/includes/search/template.php is actually responsible for that feature:
function bbp_has_search_results( $args = '' ) { global $wp_rewrite; /** Defaults **************************************************************/ $default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ); // Default query args $default = array( 'post_type' => $default_post_type, // Forums, topics, and replies 'posts_per_page' => bbp_get_replies_per_page(), // This many 'paged' => bbp_get_paged(), // On this page 'orderby' => 'date', // Sorted by date 'order' => 'DESC', // Most recent first 'ignore_sticky_posts' => true, // Stickies not supported 's' => bbp_get_search_terms(), // This is a search );
So, changing ‘post_type’ => $default_post_type to ‘post_type’ => bbp_get_topic_post_type() works. However, it’s not clearly the best practice to modify the plugin directly anyother way to override this?
yes put this in your functions file – untested but should work
add_filter ('bbp_before_get_search_terms_parse_args', 'pluus_amend_search') ; function pluus_amend_search ($args) { $args['post_type'] = bbp_get_topic_post_type() ; return $args ; }
Whoa! Thanks!
great – glad you are fixed !
Hi Robin W 🙂
The above solution does not work in 2019. Is there a new way to only search topics and replies as of now ?
the function should still work – it just searches topic.
when you say ‘The above solution does not work in 2019’ – what exactly do you mean, it errors, it display what ?
I put this:
add_filter ('bbp_before_get_search_terms_parse_args', 'pluus_amend_search') ; function pluus_amend_search ($args) { $args['post_type'] = bbp_get_topic_post_type() ; return $args ; }
in my wp-content/themes/<ThemeName>/functions.php
But Forum names (“bbp_get_forum_post_type()”) are still included in the search results on the frontend.
Only editing “plugins/bbpress/includes/search/template.php” works 🙂
hmmm. can’t say why, but if you have the template solution, not worth me working out why
The problem is, using the template solution will get overwritten when I update the the bbPress plugin…:(
no, you just put the templates in your child theme
You can copy all the templates across, but you only need to copy those that you want to change, and it is better just to do this, as then you know which you have altered.
so if you wanted to amend loop-single-forum you would do the following
create a directory on your theme called ‘bbpress’
ie wp-content/themes/%your-theme-name%/bbpresswhere %your-theme-name% is the name of your theme
find
wp-content/plugins/bbpress/templates/default/bbpress/loop-single-forum.php
Make a copy of this file, and put in in the directory called bbpress that you created above, so you end up with
wp-content/themes/%your-theme-name%/bbpress/loop-single-forum.php
bbPress will now use this template instead of the original
and you can amend thisCan I also override files in the “includes” folder (bbpress/includes/search/template.ph) in a child theme?
sorry, I’d missed that you were looking at function templates not output templates – sorry it has been a long day.
what function in that template are you changing ?
No worries 🙂 I am just happy to get help.
I want to change the function: bbp_has_search_results. Where I replace:
$default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() );
With:
$default_post_type = array(bbp_get_topic_post_type(), bbp_get_reply_post_type() );
If I could put the solution in a custom plugin it would be really awesome 🙂
ok, a better filter might be – try adding this to your functions file
add_filter ('bbp_before_has_search_results_parse_args', 'rew_amend_search') ; function rew_amend_search ($args) { $args['post_type'] = bbp_get_topic_post_type() ; return $args ; }
Really nice. Worked like a charm 🙂 !
Can I put this in a custom plugin instead of functions.php ?
I found a way to put it in a plugin 🙂
I Thought I would have to hook into ‘plugins_loaded’ like so:
add_action( 'plugins_loaded', 'bbpress_overwrite' ); function bbpress_overwrite() { add_filter ('bbp_before_has_search_results_parse_args', 'rew_amend_search') ; } function rew_amend_search ($args) { $args['post_type'] = bbp_get_topic_post_type() ; return $args ; }
But for some reason this was enough: ?
add_filter ('bbp_before_has_search_results_parse_args', 'rew_amend_search') ; function rew_amend_search ($args) { $args['post_type'] = bbp_get_topic_post_type() ; return $args ; }
not quite sure what you are trying to achieve – where are you putting this code ?
In a custom plugin I just made 🙂
then yes the code I sent is fine and works and is enough !!
Thanks again 🙂
you’re welcome
- You must be logged in to reply to this topic.