I have created a link from the dashboard, which goes to add new post, but with a category already selected for the post – but the post will not belong to this category before publishing – this is a problem, because I have some custom fields, which I have assigned to the category, and they will not be visible before the post is published.
Is there a way to asign a category to a post, before it is published, which will make what I am trying to achieve possible?
You can use the function wp_set_post_terms to set a category before the post is published. You need to get the post_id by call the global variable
$post
and get the id by$post->ID
.Here is a simple example. Change the id (2) to the id of your wanted category.
Update
If you want to change the category that will be saved when the user clicks on the link you have to add something like
?cat=2
on the dashboard-links like this:Then you can get the category bu use
$_GET['cat'];
like this: