Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Dynamically filtering bbpress topics and replies based on currently viewed Forum
- This topic has 6 replies, 2 voices, and was last updated 4 years, 8 months ago by
nickchomey18.
-
AuthorPosts
-
September 11, 2021 at 12:37 am #34671
nickchomey18
ParticipantHi ernest,
I have a search instance for bbpress topics and replies. I use a filter to toggle between showing one or the other.
I would like to be able to dynamically filter the results based on which forum is currently being viewed. Each topic and reply has a custom field “_bbp_forum_id”, so I’d like to use the “post_meta_filter” to filter for this. There is a function to return the current forum’s ID – bbp_get_forum_id() – and it works just fine, however it doesn’t seem to work in the function that I have created. I presume that it does not have access to the BuddyBoss/Press/bbpress instance for whatever reason. Stepping through it using xdebug, I can see that it returns 0 rather than the forum ID.
add_filter('asp_query_args', 'asp_filter_forum', 10, 1); function asp_filter_forum ( $args ) { $forum_id = bbp_get_forum_id(); $args['post_meta_filter'] = array( 'key' => '_bbp_forum_id', 'value' => array ($forum_id), 'operator' => '=', 'allow_missing' => false ); return $args; }So, do you have any suggestions on how I can pass a parameter to either the search instance shortcode or the asp_query_args filter?
Thanks!
September 13, 2021 at 2:02 am #34675nickchomey18
ParticipantAs it turns out, I was able to get this to work by using the $options[‘current_page_id’] field and the following snippet. You’re welcome to share it with the community – it seems to me that it is very useful for anyone using ASP with bbpress. Do you see anything about it that could be changed/improved?
add_filter('asp_query_args', 'asp_filter_forum', 10, 3); function asp_filter_forum ( $args, $search_id, $options) { if ($search_id == 4) { $forum_id = (int)$options['current_page_id']; $args['post_meta_filter'][] = array( 'key' => '_bbp_forum_id', 'value' => array ($forum_id), 'operator' => '=', 'allow_missing' => false ); } return $args; }Anyway, I am still curious as to whether it is possible to pass a custom variable/parameter/argument to an ASP shortcode? Would it involve editing the ASP source code to add another parameter to the filter, or is there another way?
September 13, 2021 at 11:56 am #34692Ernest Marcinko
KeymasterThat code looks perfectly fine. The first one did not work, as in the ajax context, the bbp_get_forum_id function did not return anything.
Well, simply adding more arguments to the shortcode will not do anything. If you want to add filters (visually) to the plugin, I recommend checking the front-end filters api.
September 13, 2021 at 3:14 pm #34700nickchomey18
ParticipantThanks! Is there anything I can do to allow functions – such as bbp_get_forum_id – to work within the ajax context? I assume that I will need to do other things like this in future snippets.
September 14, 2021 at 6:39 am #34718Ernest Marcinko
KeymasterUnfortunately no, those will not return the results you expect within the ajax context. When the ajax request is sent to WordPress, it is no longer considered as the same query as where it is originated from – but it is a separate asynchronous request – an empty pageview basically.
September 14, 2021 at 2:57 pm #34739nickchomey18
ParticipantThanks! You can please close this ticket now.
September 14, 2021 at 3:14 pm #34741nickchomey18
Participantdisregard this comment – i figured it out.
-
This reply was modified 4 years, 8 months ago by
nickchomey18.
-
This reply was modified 4 years, 8 months ago by
-
AuthorPosts
- The topic ‘Dynamically filtering bbpress topics and replies based on currently viewed Forum’ is closed to new replies.