WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [New plugin] Add a category automatically for a particular author (6 posts)

  1. Sabu K K
    Member
    Posted 1 year ago #

    I was trying to develop a plugin meant to add a category automatically while a particular author publish a post. The author should be able to select other categories as well. I used the following code, but it is not working:

    function add_category_automatically($post_ID) {
    	global $wpdb;
    	$myposts = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
    	foreach ($myposts as $newposts) {
    		if(($newposts->post_author = 1)){
    			$cat = array(3);
    			wp_set_object_terms($post_ID, $cat, 'category', true);
    		}
    	}
    }
    add_action('publish_post', 'add_category_automatically');

    Now, the problem is that while the plugin with the above code is active, when i hit the Publish button, an empty window is displaying (that ends with ...wp-admin/post.php) and not publishing the post.

  2. Tammy Hart
    Member
    Posted 1 year ago #

    Have you tried this plugin?

    http://wordpress.org/extend/plugins/default-author-category/

    It might at least give you some guidance on your implementation.

  3. Sabu K K
    Member
    Posted 1 year ago #

    Thanks for the reply. Yes, I tried the 'Default Author Category' plugin, which was not effective in my case. In fact, I was trying to develop a plugin for a similar purpose which would be useful in conjunction with auto-blogging through 'WordPress RSS Poster' plugin. While the RSS poster, fetch feed and publish the posts in one category (that is the limit for this plugin), I wanted to publish the posts to 2 categories. There are auto-bloging plugins that could do similar job and publish in as many categories one have. But since this is the best auto-blog plugin I ever tried, I wanted to use it and at the same time wanted to add the auto-posts to 2 categories. Anyway, I am working on my new plugin and if I succeed, I will post the details here soon. But any comments on the above code are most welcome.

  4. Tammy Hart
    Member
    Posted 1 year ago #

    I'm curious as to why you set $cat as an array with only one value. Do you plan on adding more?

    Also, I don't see why you have to query anything if you're already hooking into the actual act of publishing a post. I would think that you could strip out lines 2-4 and 9 and then just get the current author ID.

    But than again, I'm not familiar with this hook and I'm just guessing, trying to help as best as I can.

  5. Sabu K K
    Member
    Posted 1 year ago #

    Thank you so much. I am not good in PHP coding and the use of $cat would be by mistake in that case. Anyway, I made some modifications and surprisingly my plugin is working now! The full code is:

    <?php
    /*
    Plugin Name: Add Category by Author
    Plugin URI: http://wordpress.org/extend/plugins/add-cat-by-author/
    Description: This plugin will add a specific category to an author.
    Author: Sabu
    Version: 0.2
    Author URI: http://kksabu.blogspot.com/
    License: A "Slug" license name e.g. GPL2
    */
    
    function add_category_automatically($post_ID) {
    	global $wpdb;
    	$post = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
    	foreach ($post as $postselected) {
    		if(($postselected->post_author = 39) && (!in_category('jobs'))){
    			$cat = array(7);
    			wp_set_object_terms($post_ID, $cat, 'category', true);
    		}
    	}
    }
    add_action('save_post', 'add_category_automatically');
    
    ?>

    This plugin will add a category with id 7 and title 'Jobs' to the post (if 'Jobs' is not selected originally) when the author with id 39 save a post as Draft (which is done automatically through RSS poster).

    To develop the above plugin, I got useful hints from a previous support query posted here.

    Thank you so much for your time and help. Please let me know if anything to modify in the above coding as I am not an expert in PHP..

  6. Sabu K K
    Member
    Posted 1 year ago #

    The plugin is working fine now. The code is

    <?php
    /*
    Plugin Name: Add Category by Author
    Plugin URI: http://wordpress.org/extend/plugins/add-cat-by-author/
    Description: This plugin will add a specific category to an author.
    Author: Sabu
    Version: 0.2
    Author URI: http://kksabu.blogspot.com/
    License: A "Slug" license name e.g. GPL2
    */
    
    function add_category_automatically($post_ID) {
    	global $wpdb;
    	$post = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
    	foreach ($post as $postselected) {
    		if ($postselected->post_author != 39) {
    		/* do nothing */
    		} else {
    			$cat = array(7);
    			wp_set_object_terms($post_ID, $cat, 'category', true);
    		}
    	}
    }
    add_action('save_post', 'add_category_automatically');
    
    ?>

    This plugin will add a category (with cat id 7) for the author with author id 37. It is useful for auto posting where the auto post plugin have the limitation of posting to only one category.

    Thank you so much Tammy Hart to think more on the plugin development..

Topic Closed

This topic has been closed to new replies.

About this Topic