Skip to:
Content
Pages
Categories
Search
Top
Bottom

Grab topic IDs from forum ID


  • alexstine
    Participant

    @alexstine

    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 7 replies - 1 through 7 (of 7 total)

  • alexstine
    Participant

    @alexstine

    Hello,

    Any ideas? Maybe something like this?

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

    Thanks.


    Robin W
    Moderator

    @robin-w

    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

    @alexstine

    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.


    John James Jacoby
    Keymaster

    @johnjamesjacoby

    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

    @alexstine

    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

    @robin-w

    Suggest you try them and see.

    looks like you are learning fast.


    alexstine
    Participant

    @alexstine

    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.

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