0

Preciso identificar as categorias por slug ao adicionar um post no wordpress.

Exemplo comum e funcional:

        $post_id = wp_insert_post( array(
            'post_author'   => 1,
            'post_title'    => 'titulo',
            'post_type'     => 'post',
            'post_content'  => 'desc',
            'post_status'   => 'publish',
            'tax_input' => array( 
                  'categoria' => array( 
                    '1'  //categoria identificada por ID
                  ) 
                )
            ) );

Como eu preciso que fique:

        $post_id = wp_insert_post( array(
            'post_author'   => 1,
            'post_title'    => 'titulo',
            'post_type'     => 'post',
            'post_content'  => 'desc',
            'post_status'   => 'publish',
            'tax_input' => array( 
                  'categoria' => array( 
                    'filme'  //categoria identificada por slug
                  ) 
                )
            ) );

Existe esta possibilidade? O exemplo acima não funcionou pra mim.

1 Resposta 1

1

Você pode usar get_term_by() para recuperar o ID da categoria.

$term = get_term_by( 'slug', 'filme', 'categoria' );
$post_id = wp_insert_post( array(
    'post_author'   => 1,
    'post_title'    => 'titulo',
    'post_type'     => 'post',
    'post_content'  => 'desc',
    'post_status'   => 'publish',
    'tax_input' => array( 
        'categoria' => array( $term->term_id ) 
    )
) );

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .