This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Dynamically filtering bbpress topics and replies based on currently viewed Forum

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Dynamically filtering bbpress topics and replies based on currently viewed Forum

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #34671
    nickchomey18nickchomey18
    Participant

    Hi 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!

    #34675
    nickchomey18nickchomey18
    Participant

    As 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?

    #34692
    Ernest MarcinkoErnest Marcinko
    Keymaster

    That 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.

    #34700
    nickchomey18nickchomey18
    Participant

    Thanks! 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.

    #34718
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Unfortunately 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.

    #34739
    nickchomey18nickchomey18
    Participant

    Thanks! You can please close this ticket now.

    #34741
    nickchomey18nickchomey18
    Participant

    disregard this comment – i figured it out.

    • This reply was modified 4 years, 8 months ago by nickchomey18nickchomey18.
Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Dynamically filtering bbpress topics and replies based on currently viewed Forum’ is closed to new replies.