I'm trying to create a link that if a user clicks it goes to the add new post page with a category already selected.
I'm using custom post types
I currently have: http://www._________.com/wp-admin/post-new.php?post_type=cityattractions
That works fine on picking with custom post type to go to but I can't figure how the paramater to make a category already selected when they go to the page.
I tried "www._________.com/wp-admin/post-new.php?post_type=cityattractions&post_category=1" & "www._________.com/wp-admin/post-new.php?post_type=cityattractions&post_category=name" but they don't seem to work.
Can someone help me figure out a way that I can just create a link that will pass the parameter to make a category already selected?
Luis Abarca answers:
You can add the selected category on save_post action, i answer a similar question before here [[LINK href="http://www.wpquestions.com/question/showLoggedIn/id/3292"]]http://www.wpquestions.com/question/showLoggedIn/id/3292[[/LINK]]
You can pass the category via URL, add to a hidden field and then on save_post add that category to he post.
Zach Reed comments:
I don't really follow? Can you give some example code for my case?
Luis Abarca comments:
The other way is check the category with jQuery.
Get the category ID via $_GET.
add_action('admin_footer', 'my_admin_footer');
function my_admin_footer()
{
?>
<script>
(function($)
{
$(document).ready(function()
{
$('#in-category-<?php echo $_GET['post_category'] ?>').attr('checked', true);
});
})(jQuery);
</script>
<?php
}
Luis Abarca comments:
With jQuery, the category is visually selected, i think is more what you need.
you can also take that code, put it in a .js file and add it with:
admin_print_footer_scripts
Zach Reed comments:
That worked perfectly! Thanks! :)
Luis Cordova answers:
hmm no you need to use proper hooks for this, where do you want it can you email me at cordoval at gmail.com
Zach Reed comments:
I am making it a link on the front-end of the site. I want the user to be able to click the link and go straight to the "add new" post page (for that specific post type) and already have a category selected when they get there so they don't have to check that box.
I already figured out how to pass the post type parameter like you see above, I just can't get it to pass a category.
Luis Cordova comments:
have you tried extracting the category into the request variable? and passing it over like that?
that is standard in php right?
Zach Reed comments:
I'm not sure I know how to do that? Can you provide an example?