Skip to:
Content
Pages
Categories
Search
Top
Bottom

Conditional Statement Based on Forum ID

  • @geekyjules

    Participant

    I’m trying to write a conditional statement to auto tag new topics based on forum.

    This is what I have so far

    add_action ( 'bbp_insert_topic', 'myprefix_assign_tag_to_post' , 10, 1 );
    add_action ( 'bbp_new_topic', 'myprefix_assign_tag_to_post', 10, 1 );
    add_action ( 'bbp_edit_topic', 'myprefix_assign_tag_to_post', 10, 1 );
    
    function myprefix_assign_tag_to_post($topic_id) {
    if( bbp_is_forum ( '168' ) && bbp_current_user_can_publish_topics() ) {
    	wp_set_post_terms( $topic_id, 'introductions', 'topic-tag', true );
    }
    
    elseif( bbp_is_forum ( '1343' ) && bbp_current_user_can_publish_topics() ) {
    		wp_set_post_terms( $topic_id, 'discovery', 'topic-tag', true );
    
    }
    }
    

    The problem is, this automatically tags the new topics with “Introductions” regardless of the forum. I tried replacing ‘bbp_is_forum’ with ‘bbp_is_single_forum’, and the same thing happens. I’ve tried replacing it with ‘bbp_is_forum_archive’ and nothing happens. I’ve even tried replacing the ID with slug, with and without the single quotes, trying it with just one IF statement, and I’m not getting the desired results.

    I also tried using is_page, hoping it would be using the default WordPress call since it used in the functions.php for topics. No joy.

    So, if someone could please tell me what I should be using instead of is_forum, is_single_forum, is_etc, that would be great.

Viewing 3 replies - 1 through 3 (of 3 total)
  • @robin-w

    Moderator

    not tested as going out soon, but this should work

    add_action ( 'bbp_new_topic', 'myprefix_assign_tag_to_post', 10, 4 );
    add_action ( 'bbp_edit_topic', 'myprefix_assign_tag_to_post', 10, 4 );
    
    function myprefix_assign_tag_to_post($topic_id, $forum_id, $anonymous_data, $topic_author) {
    if( $forum_id == '168'  && bbp_current_user_can_publish_topics() ) {
    	wp_set_post_terms( $topic_id, 'introductions', 'topic-tag', true );
    }
    
    elseif($forum_id == '1343' && bbp_current_user_can_publish_topics() ) {
    		wp_set_post_terms( $topic_id, 'discovery', 'topic-tag', true );
    
    }
    }

    @geekyjules

    Participant

    Thanks @robin-w!

    That worked perfectly. Now to add the rest.

    @robin-w

    Moderator

    great – glad you are fixed

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