BuddyDev

Search

[Resolved] How To Show Posts From A Particular Taxonomy Only On User Profile

Tagged: 

  • Participant
    Level: Enlightened
    Posts: 42
    Propertytips on #30404

    Hi Brajesh,

    I created a custom post type called “Blog” and a taxonomy called “blogs” to use with the buddyblog plugin.

    My issue is that I ONLY want Posts from a one or two taxonomies to show up on the user profile, whilst the other should link to the main site when using BuddyBlog.

    Could you please help with the code…

    a code that will enable me just enter the ID’s of the taxonomies that i want to display on the user profile

    I note you have the snippet below …


    add_filter( ‘buddyblog_show_posts_on_profile’, ‘buddyblog_custom_permalinks_filter’, 10, 2 );
    function buddyblog_custom_permalinks_filter( $do_filter, $post ){
    $category = get_category_by_slug( ‘blog’ );
    $post_categories = get_the_category( $post->ID );
    $post_cat_ids = wp_list_pluck( $post_categories, ‘term_id’ );
    //if it is in blog category, show on profile
    if( in_array( $category->term_id, $post_cat_ids ) )
    return true;
    return $do_filter;
    }

    but the code seems to be only suitable if I was using the “categories” section for buddyblog

    Your help will be much appreciated

    Kind Regards

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2499
    Ravi on #30407

    Hello,

    Thank you for posting. Please try the following code it will show posts from specific term_ids

    
    /**
     * Modify query
     *
     * @param WP_Query $query Query object.
     */
    function buddydev_filter_posts_by_cat( $query  ) {
    
    	// Return if not on user screen or current component is not buddyblog.
    	if ( ! bp_is_user() || ! bp_is_current_component( 'buddyblog' ) ) {
    		return;
    	}
    
    	if ( BUDDYBLOG_ARCHIVE_SLUG !== bp_current_action() ) {
    		return;
    	}
    
    	$taxquery = array(
    		array(
    			'taxonomy' => 'genre', // Replace this by user custom taxonomy.
    			'field' => 'id',
    			'terms' => array( 32 ), // Replace by your's term_ids to want to show
    			'operator'=> 'IN'
    		)
    	);
    
    	$query->set( 'tax_query', $taxquery );
    }
    add_action( 'pre_get_posts', 'buddydev_filter_posts_by_cat' );
    
    

    Please let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 42
    Propertytips on #30409

    Hi Ravi,

    Thank you so much… I can confirm that the code works perfectly

    Kind Regards

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2499
    Ravi on #30411

    Hello,

    Thank you for acknowledgement. I am glad that I could help.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 42
    Propertytips on #30424

    Hi Ravi,

    Just noted some problems with the code on further inspection..

    The code as it is makes the navigation menus disappear from the top and sides of the buddyblog page

    Please see link to images below

    Before code implemented

    https://prnt.sc/stj87d

    After code implemented

    https://prnt.sc/stj964

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2499
    Ravi on #30425

    Hello,

    Sorry for the inconvenience. I forgot to check post_type before modify query. check the following code and let me know if it works or not.

    
    /**
     * Modify query
     *
     * @param WP_Query $query Query object.
     */
    function buddydev_filter_posts_by_cat( $query  ) {
    
    	// Return if not on user screen or current component is not buddyblog.
    	if ( ! bp_is_user() || ! bp_is_current_component( 'buddyblog' ) ) {
    		return;
    	}
    
    // Replace book with your post_type.
    	if ( BUDDYBLOG_ARCHIVE_SLUG !== bp_current_action() || 'book' != $query->get( 'post_type' ) ) {
    		return;
    	}
    
    	$taxquery = array(
    		array(
    			'taxonomy' => 'genre', // Replace this by user custom taxonomy.
    			'field' => 'id',
    			'terms' => array( 32 ), // Replace by your's term_ids to want to show
    			'operator'=> 'IN'
    		)
    	);
    
    	$query->set( 'tax_query', $taxquery );
    }
    add_action( 'pre_get_posts', 'buddydev_filter_posts_by_cat' );
    

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 42
    Propertytips on #30428

    Hi Ravi,

    Thank you very much… it works well now

    Kind Regards

The topic ‘ [Resolved] How To Show Posts From A Particular Taxonomy Only On User Profile’ is closed to new replies.

This topic is: resolved