• Hello,
    I need to find post language in this function (used in functions.php) to set $lang and then define $slug_taccuino:

    function automatically_name_taccuino($post_id, $post, $update) {
    $desired_type = ‘taccuino’;
    if(( !in_array( $data[‘post_status’], array( ‘draft’, ‘pending’, ‘auto-draft’ ) ) ) && $desired_type === $post->post_type) {
    if ( $lang === ‘it’ ) {
    $slug_taccuino = ‘taccuino’;
    }
    if ( $lang === ‘en’ ) {
    $slug_taccuino = ‘notebook’;
    }
    if ( $lang === ‘fr’ ) {
    $slug_taccuino = ‘cahier’;
    }

    $keep_title = get_the_title();
    $keep_post_id = get_the_ID();
    $date_slug = get_the_date(‘Y-m-d’);
    $post_title_parts = array();
    $post_title_parts[] = $keep_title;
    $post_title_parts[] = $slug_taccuino;
    $post_title = implode(‘ – ‘, array_filter(array_map(‘trim’, $post_title_parts)));
    $post_name = sanitize_title($post_title, $post_id);
    global $wpdb;
    $wpdb->update($wpdb->posts, array(‘post_name’ => $post_name), array(‘ID’ => $post_id));
    }
    }
    add_action(‘wp_insert_post’, ‘automatically_name_taccuino’, 11, 3);

    Any help is appreciated!

Viewing 1 replies (of 1 total)
  • Thread Starter Giovanni Invernizzi

    (@giovanni-paperplane)

    Answer 🙂

    function automatically_name_taccuino($post_id, $post, $update) {
    $desired_type = ‘taccuino’;
    if(( !in_array( $data[‘post_status’], array( ‘draft’, ‘pending’, ‘auto-draft’ ) ) ) && $desired_type === $post->post_type) {
    $lang = pll_get_post_language($post_id);
    if ( $lang === ‘it’ || $lang == ” ) {
    $slug_taccuino = ‘taccuino’;
    }
    if ( $lang === ‘en’ ) {
    $slug_taccuino = ‘notebook’;
    }
    if ( $lang === ‘fr’ ) {
    $slug_taccuino = ‘cahier’;
    }

    $keep_title = get_the_title();
    $keep_post_id = get_the_ID();
    $date_slug = get_the_date(‘Y-m-d’);
    $post_title_parts = array();
    $post_title_parts[] = $keep_title;
    $post_title_parts[] = $slug_taccuino;
    $post_title = implode(‘ – ‘, array_filter(array_map(‘trim’, $post_title_parts)));
    $post_name = sanitize_title($post_title, $post_id);
    global $wpdb;
    $wpdb->update($wpdb->posts, array(‘post_name’ => $post_name), array(‘ID’ => $post_id));
    }
    }
    add_action(‘wp_insert_post’, ‘automatically_name_taccuino’, 11, 3);

Viewing 1 replies (of 1 total)
  • The topic ‘Polylang – get post language when using wp_insert_post’ is closed to new replies.