I want to have a link to create a new post that sets the category also.
I have tried wp-admin/post-new.php?post_category=12
and wp-admin/post-new.php?cat=12
, but neither worked. I also tried using the name rather than the id of the category; which also had no affect.
How do I create a link to a new post with a default category?
Dave James Miller over at GitHub nailed this one for me. None of the work is from me, I’m just posting his code wrapped into a plguin since it works perfectly as advertised:
Hook into
wp_insert_post
, test the post status forauto-draft
, and the URL for aGET
parameter.But first we need a helper function to get and sanitize the
GET
parameter:Now the auto-draft handler:
This works only if
get_default_post_to_edit()
was called with the second parameter$create_in_db
set toTRUE
. To catch the other case you have to filter the optiondefault_category
:Now you can use the parameter
post_cat
to pass a comma separated list of category slugs:See also:
I think you can go about the default option
default_category
and filteroption_default_category
this, if the url have a param for the category, like this example source. Use it as plugin, test it. Was write from scratch and not tested.The url param is
post_cat
and you can set the category, like this url:/wp-admin/post-new.php?post_cat=categoryname
I realize this has already been answered, but I wanted to add my own take. I’ve added it to a gist here https://gist.github.com/malcalevak/ba05b4fbed0c6e33ac8c18c1451bd857
To save you the hassle, though, here’s the code:
Similar to all the others, you’d trigger it via /wp-admin/post-new.php?category=categoryname
FYI, if you’re using Advanced Custom Fields, like @Aphire, this WILL work.