1

I have a custom post type that has 6 available categories. The categories are used as column headings in a table with their corresponding posts in that column. I want to be able to click on the heading (category name) which will be an anchor tag and open the add new post page in the admin area with the category selected. Does anyone have any ideas on this please?

| improve this question | | | | |
0

in my exemple, "CPT" stands for the slug of your custom post type.

First step :

Using the link of "create new CPT" to add the information of category. for exemple the result would look like http://server/wp-admin/post-new.php?post_type=CPT&category=potatoes

Second step :

Using the hook save_post_CPT to preset the category. it would be something like that :

add_action("save_post_CPT", function ($post_ID, $post, $update) {

    if ($update) {
        // it's not a new object
        return;
    }

    if (!isset($_GET["category"])) {
        return;
    }

    // setting the category
    wp_set_object_terms($post_ID, $_GET["category"], "category", TRUE);


}, 10, 3);
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.