• I have this piece of code and it works great.

    I can create a shortcut like so ..
    https://mysite.net/site/wp-admin/post-new.php?post_title=Journal&category=Journal

    And it sets the title to journal and category to journal.

    function set_category () {
    
        global $post;
      //Check for a category parameter in our URL, and sanitize it as a string
        $category_slug = filter_input(INPUT_GET, 'category', FILTER_SANITIZE_STRING, array("options" => array("default" => 0)));
    
      //If we've got a category by that name, set the post terms for it
        if ( $category = get_category_by_slug($category_slug) ) {
            wp_set_post_terms( $post->ID, array($category->term_id), 'category' );
        }
    
    }
    
    //hook it into our post-new.php specific action hook
    add_action( 'admin_head-post-new.php', 'set_category', 10, 1 );

    But I would also like to set the post status to private. The thing is not for every category.

    I would like to set the post to private only when I use the link to set the category. If this is too much or cant be done .. i dont mind hard coding the Journal in there or something.

    thanks for your help

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Not possible via URL parameters, you need to get into coding.

    Almost anything about a post can be altered through the ‘wp_insert_post_data’ filter, including setting the status to private and setting a password. But there is nothing in the passed data that would indicate the category was set by URL parameter and not by simply selecting it during edits. Your code should be able to determine this by checking the original request in the $_SERVER array.

    Thread Starter rleon

    (@rleon)

    hi bc

    Sorry I was not clear. I get that .. but I didnt want to pass it through the URL.

    My goal was to either set the post to private in the category journal or when setting the category in the above code .. set the status to private.

    Moderator bcworkz

    (@bcworkz)

    OK, my proffered solution would still apply. It’s the best place to set status by code. The status belongs to the post regardless of where it appears on site. You cannot only set private for a category view, it is always applied to all views. But you can set the status based on specific criteria when the post is saved.

    There are some minor complications in my solution to resolve. About checking $_SERVER, the current request is not where to look. The URL parameter should be in the referrer field, but I’ve not verified this. If that doesn’t get it, hook an action that fires on editor load. Check for the request URL parameter there and set a flag that’s saved somewhere where the ‘wp_insert_post_data’ callback can check for it. Save in a session, cookie, transient, anything that persists between requests.

    If you want to check if the category was set by URL, but subsequently removed during edit, you also need to check the second passed array’s ‘post_category’ element.

    IMO, your code should only set private status of new posts. Post updates should be ignored. New vs. update can be checked in the second passed array. If the ‘ID’ element of that array is empty, it’s a new post. If it’s assigned an integer, it’s an update.

    That second passed array is the original, unprocessed data that is passed to the wp_insert_post() function. The first passed array is the processed data that will become the post’s new data. If you var_dump these arrays along with $_SERVER for inspection, it should become clear how to bring the various criteria together to do what you want.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add private status to function’ is closed to new replies.