Skip to:
Content
Pages
Categories
Search
Top
Bottom

Grab topic IDs from forum ID

  • @alexstine

    Participant

    Hello,

    I have a plugin that is used to index search results in to Amazon CloudSearch. I’ve written the below code to exclude private forums from search results. Now I need some code to automatically exclude all topics inside a private forum.

    function exclude_private_forums_search() {
    	$forum_id = bbp_get_forum_id();
    	if (get_post_status($forum_id) == 'private') {
    		add_post_meta($forum_id, 'acs_exclude', 1, true );
    	} else {
    		delete_post_meta($forum_id, 'acs_exclude');
    	}
    }
    add_action('bbp_new_forum', 'exclude_private_forums_search' );
    add_action('bbp_edit_forum', 'exclude_private_forums_search' );

    Is there anyway I can get a list of topic IDs inside a forum ID?

    Running latest version of bbPress and WordPress.

    Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • @alexstine

    Participant

    Hello,

    Any ideas? Maybe something like this?

    <?php echo bbp_get_forum_id(bbp_get_topic_id()); ?>

    Thanks.

    @robin-w

    Moderator

    Is there anyway I can get a list of topic IDs inside a forum ID?

    sorry missed this first time round.

    you’ll want bbp_has_topics which you’ll find in

    \includes\topics\template.php

    line 140

    function bbp_has_topics( $args = '' )

    you can call it with a forum id eg

    <?php $query = bbp_has_topics( array( 'post_parent' => '2579') ); ?>

    this will return an array of topics from forum ID 2579 which you can then work with

    @alexstine

    Participant

    Hello @robin-w,

    I still need to get this straight. Maybe something like this would work?

    /*Remove all topics from Amazon CloudSearch if topic is in a private forum*/
    /*Private Forums are added as an integration through a plugin*/
    /*Amazon CloudSearch is added as integration through a plugin*/
    function exclude_topics_in_private_forum() {
    $forum_id = bbp_get_forum_id();
    $query = bbp_has_topics( array( 'post_parent' => $forum_id) );
    foreach($query as $q) {
    if(is_private_forum($forum_id) ) {
    update_post_meta($q, 'exclude', 1);
    } elseif(!is_private_forum($forum_id) ) {
    delete_post_meta($q, 'exclude');
    }
    }
    }
    add_action('bbp_new_forum', 'exclude_topics_in_private_forum' );
    add_action('bbp_edit_forum', 'exclude_topics_in_private_forum' );

    It is important to note that I am using custom plugins to pull off a lot of the private forum abilities that way I can control user access better. I think the code snippet above will do it. Thoughts?

    Thanks.

    @johnjamesjacoby

    Keymaster

    You could try bbp_get_all_child_ids(), though it does a direct database query, and has no limit on the results that get returned, so in cases where there are many topics to chug through, it can be a bit much.

    @alexstine

    Participant

    Hello,

    Okay, I like something that doesn’t query the DB to much. Would something like this work? I’m somewhat new to arrays and post_meta, but trying to learn fast.

    /*Remove all topics from Amazon CloudSearch if topic is in a private forum*/
    /*Private Forums are added as an integration through a plugin*/
    /*Amazon CloudSearch is added as integration through a plugin*/
    function exclude_topics_in_private_forum() {
    $forum_id = bbp_get_forum_id();
    $query = bbp_get_all_child_ids($forum_id, 'post');
    foreach($query as $q) {
    OR
    foreach($query as $key => $q) {
    if(is_private_forum($forum_id) ) {
    update_post_meta($q, 'exclude', 1);
    OR
    update_post_meta($key, 'exclude', 1);
    } elseif(!is_private_forum($forum_id) ) {
    delete_post_meta($q, 'exclude');
    }
    }
    }
    add_action('bbp_new_forum', 'exclude_topics_in_private_forum' );
    add_action('bbp_edit_forum', 'exclude_topics_in_private_forum' );

    How does that look? Any suggestions?

    Thanks.

    @robin-w

    Moderator

    Suggest you try them and see.

    looks like you are learning fast.

    @alexstine

    Participant

    Looking back on it now, I never did have the development time to make this work before I discontinued CloudSearch, but this looks to be the best code if anyone’s brave enough to try it in the future. 🙂

    function exclude_topics_in_private_forum() {
    $forum_id = bbp_get_forum_id();
    $query = bbp_get_all_child_ids($forum_id, 'post');
    foreach($query as $key => $q) {
    if(is_private_forum($forum_id) ) {
    update_post_meta($key, 'exclude', 1);
    } elseif(!is_private_forum($forum_id) ) {
    delete_post_meta($q, 'exclude');
    }
    }
    }
    add_action('bbp_new_forum', 'exclude_topics_in_private_forum' );
    add_action('bbp_edit_forum', 'exclude_topics_in_private_forum' );

    Thanks for the help.

    @arthurlutte

    Participant

    It is working perfectly, except that you need to specify the good post type (here it is ‘topic’).

    $query = bbp_get_all_child_ids($forum_id, 'topic');

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.
Skip to toolbar