• Hello,

    My website structure is really complex. In short, here is what I have done and what I want to do.

    I. What I have successfully done…

    For each of my products, I have a related forum with its own post type (cma_thread) and category (cma_category). Each time I publish a post for a product, its related post on the forum (a standard ‘Please read this post before submitting in this forum’) is automatically created and set to a category which is the post title, sanitized. Everything is done with an “add_action on save post”.

    II. What I can’t actually achieve…

    Kinda the same process for my 4 featured images per post. Because each image is published as an “attachment” post type, and since I have added the category support, I just have to assign a category (the sanitized title, again) for the 4 featured images.

    But the problem here is that no title are defined until I save first my post. And this means I can’t assign a function on “save_post”.

    Then, I have probably to add an action on upload or something similar… but I’m really stucked actually. Do you guys have some tips to give me on the way to proceed?

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the same callback you’re using for the main post to get related image attachments. Use get_attached_media() or get_children() for this. You’ll have the title now, so categories may be added to the child attachments.

    Thread Starter moxymore

    (@moxymore)

    Thank you for the clarification. But I need a proper example on how to treat this array, because I probably have to use foreach in order to achieve what I want. The thing is… I always failed to use foreach()…

    With get_attached_media( ‘image’ ); I have the following array :

    Array
    (
        [214] => WP_Post Object
            (
                [ID] => 214
                ...
            )
    
        [213] => WP_Post Object
            (
                [ID] => 213
                ...
            )
        [216] => WP_Post Object
            (
                [ID] => 216
                ...
            )
    
        [215] => WP_Post Object
            (
                [ID] => 216
                ...
            )
    )

    Now, how can I write my function in order to tell it : “for each post object => do a wp_set_object_terms( $post_id, array($cma_slug_sanitized), ‘cma_category’ );”?

    This sounds kinda a newbie question, but when some principle are immediately understood, other aren’t, and this is the case for “foreach” (php.net didn’t helped me for it).

    Thank you in advance.

    [EDIT]

    I have tried with the 2 following codes :

    $medias = get_attached_media( 'image' );
    			foreach($media as $media):
    			  wp_set_object_terms( $media, array($cma_slug_sanitized), 'cma_category' );
    			endforeach;
    $medias = get_attached_media( 'image' );
    			foreach($media as $media):
    			  wp_set_object_terms( $media->ID, array($cma_slug_sanitized), 'cma_category' );
    			endforeach;
    • This reply was modified 7 years, 11 months ago by moxymore.
    Thread Starter moxymore

    (@moxymore)

    Allright, I had a typo error in “foreach($media as $media)” where I have forgotten the plural => “foreach($medias as $media)”

    I have finally understood this foreach ?? Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Assign automatically a category to an attachment on upload’ is closed to new replies.