• jirenu

    (@jirenu)


    I found this script to functions

    function wpse_248054(){
        $custom_field_name = 'Genre';
        $posts = new WP_Query(
            array(
                'posts_per_page' => -1,
                'post_type'=> 'post',
            )   
        );
        foreach($posts->posts as $post){
            $meta = get_post_meta($post->ID, 'Genre', true);
            if(! empty( $meta )){
                $term_id = wp_create_category($meta, 3); // 2nd argument, parent id (optional)
                $new_post_cat = wp_set_post_terms( $post->ID, $term_id, 'category', true );
            }
        }
    }
    add_action('admin_init', 'wpse_248054');

    this auto add custom field to category, the problem is the meta key have a multiple values, like : Action, Drama, War
    and this script don’t separate values with comma and creat a category “Action, Drama, War” all together. who can i separate values ?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Use the PHP function “explode” to separate a comma delimited string.

    https://www.php.net/manual/en/function.explode.php

    Thread Starter jirenu

    (@jirenu)

    thanks for reply
    im noob in php, i dont how do this!

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The function returns an array of strings given a comma separated string. You can either walk through the array and create categories for each or pick the zeroth element.

    But since you don’t understand PHP, let’s start by asking this: What are you trying to accomplish? Use words, not code.

    Thread Starter jirenu

    (@jirenu)

    So, omdbapi generate information about movies to meta keys.
    Like, year, actors, director, genre…

    i wish filed “Genre” auto add to categories.
    example:

    View post on imgur.com

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You’re adding categories? Or are you adding custom fields to categories? What is “omdbapi”

    It looks like you’ve created custom post types. Am I correct? Are you using ADvanced Custom Fields or something like that?

    Thread Starter jirenu

    (@jirenu)

    No, is a theme with omdbapi generator (imdb/themoviedb informations).
    They create all meta keys automatic but dont add genres to categories, i put manual.

    • This reply was modified 5 years ago by jirenu.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I guess I don’t understand what you mean by “add genres to categories”. Are genres child categories?

    Thread Starter jirenu

    (@jirenu)

    “Convert” this custom field to categories automatic

    View post on imgur.com

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Then why are you adding meta fields rather than adding the post(s) to a category?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘auto custom field to category’ is closed to new replies.