• I have a snippet of code running to remove a post from a category after a specified length of time, however I would like to exclude an author from this function. I have tried a few things to accomplish this with no success. I am really unsure how to accomplish this.

    Here is the code use to remove the post from a category:

    function auto_cat_remove() {
        global $post;
        wp_schedule_single_event( time() + 20, 'remove_news_cat_event', array( $post->ID ) );
    }
    
    function remove_news_cat_func( $post_id ) {
        global $wpdb;
        $category_id = 598;
        $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $post_id, $category_id ) );
    
    }
    
    add_action( 'wp_insert_post', 'auto_cat_remove' );
    add_action( 'remove_news_cat_event', 'remove_news_cat_func' );
Viewing 5 replies - 1 through 5 (of 5 total)
  • RossMitchell

    (@rossmitchell)

    In your function ‘auto_cat_remove’ where you have $post available, test the author and only schedule the removal if the author is not the protected one.

    Thread Starter Chris Whiteley

    (@thewhite)

    Thanks for responding!
    I’m not exactly sure I fully understood your suggestion. I tried to interpreting it as this:

    function auto_cat_remove() {
        global $post;
    	$authorID = get_the_author_meta('ID');
    		if(!$authorID = 21) {
        	wp_schedule_single_event( time() + 20, 'remove_news_cat_event', array( $post->ID ) );
    	}
    }

    But it was not successful. The function still fires not matter what author created the post.

    RossMitchell

    (@rossmitchell)

    You were close.
    Try this:
    if( $authorID != 21 )

    I am surprised you didn’t get an error message. Your expression would be evaluated as:
    ( ( ! $authorID ) = 21 )
    Which would have the value of 21, which is TRUE

    If This revision does not work, then display your $author_ID to make sure it matches the post.

    Thread Starter Chris Whiteley

    (@thewhite)

    I gave your suggestion a try, but the function still fires no matter what the author ID is. I’ve confirmed the correct ID.

    Is this not working because I am running this in functions.php instead of a theme page?

    RossMitchell

    (@rossmitchell)

    Have you confirmed that the correct ID is fetched in the context of your function.
    You have $post as a global but are not using it. Is there another way to fetch the author ID from the $post ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Conditional Statements in Functions.php’ is closed to new replies.