Home Forums Plugin Support WP User Frontend Pro Post Not Assigning to Category

This topic is: resolved

This topic contains 8 replies, has 4 voices, and was last updated by  Rogier 5 years ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #13847

    Andrew
    Member
    Post count: 5

    Hello,

    I have added a category text field to my post submission form. When a user starts typing the category appears in drop down below. I can then select the category. I also have a tags text field that does a similar thing (drop down only appears if the tag has been used before). What's strange is that the post category doesn't get assigned but the post tags do!

    My settings for the category are:

    Type: Text Input
    Order by: Name
    Order: ASC
    Selection Type: Include

    I'm not sure what to put in the selection terms but I've tried both with the categeory IDs and without!

    Why is the category not being attached to the post? I have the post going to pending review and when I confirm the post then it get assigned to the default category and not the category selected in the text input in the form submission.

    Help please! I have purchased this and it's just not doing what it should.

    #13852

    Mahi
    Member
    Post count: 1555

    You sure you used right post type and right category/Taxonomies field?

    #13855

    Andrew
    Member
    Post count: 5

    Hello Mahi,

    Yes, I'm using Post Type: Post and yes the category section which when saved turns to Taxonomy. The categories appear when I start typing and I can select one, it just doesn't appear when I submit it.

    However, it works when I select a tag which is the weird thing. I can provide you access if you could have a look? Will obviously reply privately.

    Thanks,
    Andy

    #13866

    Tareq Hasan
    Keymaster
    Post count: 2831

    Hey Andy, categories will not work this way I guess. You need to provide category ID's to update the categories as the category taxonomy is hierarchical. Tags aren't hierarchical and works with text input, so it works.

    #13867

    Andrew
    Member
    Post count: 5

    Is there no way then this will work with inputting text? It finds the categories fine and separates them with a comma. Is there not a tweak with the code we could do? I need to be able to write multiple categories in.

    #13899

    Andrew
    Member
    Post count: 5

    Any idea how this can be done? Surely, if the categories are appearing, it's possible to get the ID of that category. Really need this feature.

    Your help would be appreciated.

    Thanks,
    Andy

    #13911

    Tareq Hasan
    Keymaster
    Post count: 2831

    Sorry for replying late.

    I was tinkering with this, try this code. Paste this to your functions.php
    [php]
    function wpufe_set_post_category( $post_id ) {
    if ( isset( $_POST[‘category'] ) ) {
    $categories = $_POST[‘category'];

    $hierarchical = array_map( ‘trim', array_map( ‘strip_tags', explode( ‘,', $categories ) ) );
    wp_set_object_terms( $post_id, $hierarchical, ‘category' );
    }

    }

    add_action( ‘wpuf_add_post_after_insert', ‘wpufe_set_post_category' );
    add_action( ‘wpuf_edit_post_after_update', ‘wpufe_set_post_category' );
    [/php]

    #13926

    Andrew
    Member
    Post count: 5

    Hi Tareq,

    That is brilliant! Works perfectly ๐Ÿ™‚ Thank you so much for your help, much appreciated!

    Hope you have a great Christmas & a happy new year!

    Thanks again,
    Andy

    #16110

    Rogier
    Member
    Post count: 2

    Hi Team,

    How does it work if I want to have it work with the Product Categories from WooCommerce? I have tried, but it only takes my child category and doesn't automatically add the parent category.

    I have tried the following:

    function wpufe_set_post_category( $post_id ) {
    if ( isset( $_POST[‘product_cat'] ) ) {
    $categories = $_POST[‘product_cat'];

    $hierarchical = array_map( ‘trim', array_map( ‘strip_tags', explode( ‘,', $categories ) ) );
    wp_set_object_terms( $post_id, $hierarchical, ‘product_cat' );
    }

    }

    add_action( ‘wpuf_add_post_after_insert', ‘wpufe_set_post_category' );
    add_action( ‘wpuf_edit_post_after_update', ‘wpufe_set_post_category' );

    But as said this doesn't work.

    Thanks in advanced for the support.

    Best regards,

    Rogier

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Post Not Assigning to Category’ is closed to new replies.