BuddyDev

Search

Buddyblog assign taxonomy on update

  • Participant
    Level: Guru
    Posts: 907
    Tosin on #55260

    Hello

    I have a custom taxonomy called content_type where by (Sponsored Posts) is a taxonomy option, now ive hidden/excluded this (Sponsored Posts) taxonomy option in the buddyblog settings but im using this code below to auto assign the (Sponsored Posts) taxonomy when a post has been paid for on checkout.

     /**
     * Assign (Sponsored Post) category to BuddyBlog pay per post articles on checkout.
     */
    add_action( 'bblpro_ppp_post_purchase_completed', function( $post_id ) {
    	$post_type = get_post_type( $post_id );
    	if ( 'post' !== $post_type ) {
    		return;
    	}
    	// Get taxonomy term.
    	$newterm = get_term_by( 'name', 'Sponsored Posts', 'content_type' );
    	if ( ! $newterm ) {
    		return;
    	}
    	// Assign term to post.
    	wp_set_post_terms( $post_id, $newterm->term_id, 'content_type', true );
    } ); 

    The problem now is if I try to edit and update the published post later, the previously assigned (Sponsored Posts) taxonomy is removed after clicking the update button

    How can I enable the previous assigned paid post to still remain intact when editing the post even though the taxonomy is hidden

  • Participant
    Level: Guru
    Posts: 907
    Tosin on #55287

    Gentle reminder sir thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24968
    Brajesh Singh on #55290

    Hi Tosin,
    I am sorry but I do not understand this part

    I have a custom taxonomy called content_type where by (Sponsored Posts) is a taxonomy option, now ive hidden/excluded this (Sponsored Posts) taxonomy option in the buddyblog settings but im using this code below to auto assign the (Sponsored Posts) taxonomy when a post has been paid for on checkout.

    Please help me understand it clearly(which post type, terms, taxonomies are relevant, are you attaching multiple taxonomy terms with the post?)

    Thank you
    Brajesh

  • Participant
    Level: Guru
    Posts: 907
    Tosin on #55403

    1. I created a custom taxonomy called CONTENT TYPE where by (Sponsored Posts) is an option.

    2. I now disabled the (Sponsored Posts) option in the buddyblog settings because I don’t want users to be select this option when posting.

    3. I only want the (Sponsored Posts) option to be automatically assigned only to featured paid post using the code I shared above. IM USING PAY PER POST ADDON

    THE PROBLEM

    1. The problem now is with the retention/removal of the (Sponsored Posts) term to published paid post when users try to edit and republish a paid post.

    2. If I edit a paid post the (Sponsored Posts) term is removed after clicking the (UPDATE) button.

    3. When editing a paid post the term (Sponsored Posts) should not be removed as the term was initially assigned using the code I provided

  • Keymaster
    (BuddyDev Team)
    Posts: 24968
    Brajesh Singh on #55418

    Hi Tosin,
    Thank you for the details.

    I understand your need for classifying the posts as Sponsored Posts when it is a paid post. The problem with your current approach is you need a hidden category which should remain selected on purchased posts. When the post is updated, BuddyBlog checks which terms were selected, since your term is hidden in BuddyBlog Pro, It is marked as not selected.

    You may want to hook to bblpro_post_submitted and bblpro_post_updated and apply the term by checking if the post is paid.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 907
    Tosin on #55424

    Thanks Brajesh

    This is the code I used can you confirm if its accurate

     /**
     * Assign (Sponsored Post) category to BuddyBlog pay per post articles on checkout.
     */
    function assign_sponsored_term_if_paid( $post_id, $form_id ) {
    	if ( 'post' !== get_post_type( $post_id ) ) {
    		return;
    	}
    
    	// Check if post is a paid post by checking if order ID exists.
    	$order_id = get_post_meta( $post_id, '_bblpro_ppp_order_id', true );
    	if ( ! $order_id ) {
    		return;
    	}
    
    	// Get the Sponsored Posts term.
    	$term = get_term_by( 'name', 'Sponsored Posts', 'content_type' );
    	if ( ! $term || is_wp_error( $term ) ) {
    		return;
    	}
    
    	// Assign Sponsored Posts term to the post (merge with existing).
    	wp_set_post_terms( $post_id, array( $term->term_id ), 'content_type', true );
    }
    add_action( 'bblpro_post_submitted', 'assign_sponsored_term_if_paid', 20, 2 );
    add_action( 'bblpro_post_updated', 'assign_sponsored_term_if_paid', 20, 2 );  

    Thanks

  • Participant
    Level: Guru
    Posts: 907
    Tosin on #55425

    This is the code that worked for me

    /**
     * Assign "Sponsored Posts" content type if post is marked as sticky.
     */
    function assign_sponsored_term_if_sticky( $post_id, $form_id, $post_data ) {
    	// Check if post is sticky.
    	if ( ! is_sticky( $post_id ) ) {
    		return;
    	}
    
    	// Get the "Sponsored Posts" term in 'content_type' taxonomy.
    	$term = get_term_by( 'name', 'Sponsored Posts', 'content_type' );
    	if ( ! $term || is_wp_error( $term ) ) {
    		return;
    	}
    
    	// Append the term to the post without removing other terms.
    	wp_set_post_terms( $post_id, array( $term->term_id ), 'content_type', true );
    }
    add_action( 'bblpro_post_submitted', 'assign_sponsored_term_if_sticky', 20, 3 );
    add_action( 'bblpro_post_updated', 'assign_sponsored_term_if_sticky', 20, 3 );  

    I dont know why this code below did not work

     /**
     * Reassign "Sponsored Posts" term when a paid post is submitted or updated.
     */
    function assign_sponsored_term_if_paid( $post_id, $form_id, $post_data ) {
    	// Check if the post is paid.
    	if ( ! bbl_ppp_get_post_order_id( $post_id ) ) {
    		return;
    	}
    
    	// Get the "Sponsored Posts" term.
    	$term = get_term_by( 'name', 'Sponsored Posts', 'content_type' );
    	if ( ! $term ) {
    		return;
    	}
    
    	// Append the term without removing existing ones.
    	wp_set_post_terms( $post_id, array( $term->term_id ), 'content_type', true );
    }
    add_action( 'bblpro_post_submitted', 'assign_sponsored_term_if_paid', 20, 3 );
    add_action( 'bblpro_post_updated', 'assign_sponsored_term_if_paid', 20, 3 );
     
  • Keymaster
    (BuddyDev Team)
    Posts: 24968
    Brajesh Singh on #55432

    Hi Tosin,
    Thank you for the report. I am not sure why the second one did not work for you but the first one did(First one is only feasible when the second must be true).

    Do you use xdebug? If yes, I will suggest stepping through the code while submitting form to check the workflow.

    Regarads
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved