• Good morning I’m using the latest version of WordPress and one of my present when using the function to insert a post into the database using php programming these are duplicated me, this is that instead of creating only one entry creates two !! ! and do not know how to fix it.

    The code:

    $post = array(
    ‘post_content’ => $descripcion,
    ‘post_title’ => $_POST[‘nombre’],
    ‘post_author’ => $userid,
    ‘post_status’ => ‘draft’,
    ‘post_excerpt’ => $rest,
    ‘post_type’ => ‘ait-item’
    );

    $identificador = wp_insert_post( $post );
    ————————————————————————————————–

    Buenos días ando usando las ultimas versiones de WordPress y en una de mis programaciones actuales al usar la función para insertar un post en la base de datos mediante php estos se me duplican, esto es que en vez de crear solo una entrada crea dos!!! y no se como solucionarlo.

    El codigo:

    $post = array(
    ‘post_content’ => $descripcion,
    ‘post_title’ => $_POST[‘nombre’],
    ‘post_author’ => $userid,
    ‘post_status’ => ‘draft’,
    ‘post_excerpt’ => $rest,
    ‘post_type’ => ‘ait-item’
    );

    $identificador = wp_insert_post( $post );

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello @luis Alberto Agea Duran,

    it would be helpful to know in what context you are running wp_insert_post? Are you calling this function after a specific action? Can you share more of your code?

    Thread Starter ImageLuis Alberto Agea Duran

    (@disenorapido)

    @implenton the function is within a plugin and office with a shortcode, complete code is as follows:

    if(isset($_POST[‘enviar’])){

    $cu = wp_get_current_user();
    $userid = $cu->ID;

    $category = $_POST[‘categoria’];
    $name = $_POST[‘localizacion’];

    $descripcion = $_POST[‘descripcion’];
    $rest = substr($descripcion, 0, 80);

    $title = $_POST[‘nombre’];

    $post = array(
    ‘post_content’ => $descripcion,
    ‘post_title’ => $title,
    ‘post_author’ => $userid,
    ‘post_status’ => ‘draft’,
    ‘post_excerpt’ => $rest,
    ‘post_type’ => ‘ait-item’
    );

    $identificador = wp_insert_post( $post );

    if(!empty($category)){

    $categoria4=”SELECT * FROM ade_re_term_taxonomy p,ade_re_terms t WHERE p.term_id=t.term_id && t.name=’$category’ && taxonomy=’ait-items'”;
    $query_cat4= mysqli_query($con, $categoria4);
    if (!$query_cat4){echo(“Error al leer los datos de las categorias: 02 ” . mysqli_error($con));}
    $row5 = mysqli_fetch_array($query_cat4);

    $termtaxonomi2 = $row5[‘term_taxonomy_id’];

    $ins1 = “insert into ade_re_term_relationships (object_id,term_taxonomy_id)VALUES ($identificador,$termtaxonomi2)”;
    $insql = mysqli_query($con, $ins1);
    if (!$insql){echo(“Error al insertar los datos de las categorias 15: ” . mysqli_error($con));}

    }

    //direccion a buscar
    $direccion= urlencode(sanitize_text_field($_POST[‘direccion’]));

    //Buscamos la direccion en el servicio de google
    $geocode=file_get_contents(‘http://maps.google.com/maps/api/geocode/json?address=’.$direccion.’&sensor=false’);

    //decodificamos lo que devuelve google, que esta en formato json
    $output= json_decode($geocode);

    //Extraemos la informacion que nos interesa
    $lat = $output->results[0]->geometry->location->lat;
    $long = $output->results[0]->geometry->location->lng;

    //la imprimimos

    $map = array(
    ‘address’ => sanitize_text_field($_POST[‘direccion’]),
    ‘latitude’ => $lat,
    ‘longitude’ => $long,
    ‘streetview’ => 0,
    ‘swheading’ => 0,
    ‘swpitch’ => 0,
    ‘swzoom’ => 1.0000000000000002
    );

    $contenido = array(
    ‘subtitle’ => $_POST[‘subtitulo’],
    ‘featuredItem’ => 0,
    ‘map’ => $map,
    ‘telephone’ => $_POST[‘telefono’],
    ’email’ => $_POST[’email’],
    ‘showEmail’ => 1,
    ‘contactOwnerBtn’ => 1,
    ‘web’ => 0,
    ‘displayOpeningHours’ =>1,
    ‘openingHoursMonday’ => $_POST[‘motivo’],
    ‘openingHoursTuesday’ => $_POST[‘trabajadores’],
    ‘openingHoursWednesday’ => $_POST[‘actividad’],
    ‘openingHoursThursday’ => $_POST[‘constitucion’],
    ‘openingHoursFriday’ => $_POST[‘formajuridica’],
    ‘openingHoursSaturday’ => $_POST[‘precio’],
    ‘openingHoursSunday’ =>$_POST[‘dni’]
    );

    add_post_meta( $identificador, ‘_ait-item_item-data’, $contenido );

    if(!empty($name)){

    $categoria1=”SELECT * FROM ade_re_term_taxonomy p,ade_re_terms t WHERE p.term_id=t.term_id && t.name=’$name'”;
    $query_cat= mysqli_query($con, $categoria1);
    if (!$query_cat){echo(“Error al leer los datos de las ubicaciones: 02 ” . mysqli_error($con));}
    $row4 = mysqli_fetch_array($query_cat);
    $termtaxonomi = $row4[‘term_taxonomy_id’];

    $ins1 = “insert into ade_re_term_relationships (object_id,term_taxonomy_id) VALUES ($identificador,$termtaxonomi)”;
    $insql = mysqli_query($con, $ins1);
    if (!$insql){echo(“Error al insertar las ubicaciones 15: ” . mysqli_error($con));}

    }
    // Subir imagenes a wordpress

    $post_id= $identificador;

    $upload = wp_upload_bits( $_FILES[‘image’][‘name’], null, file_get_contents( $_FILES[‘image’][‘tmp_name’] ) );

    $wp_filetype = wp_check_filetype( basename( $upload[‘file’] ), null );

    $wp_upload_dir = wp_upload_dir();

    $attachment = array(
    ‘guid’ => $wp_upload_dir[‘baseurl’] . _wp_relative_upload_path( $upload[‘file’] ),
    ‘post_mime_type’ => $wp_filetype[‘type’],
    ‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename( $upload[‘file’] )),
    ‘post_content’ => ”,
    ‘post_status’ => ‘inherit’
    );

    $attach_id = wp_insert_attachment( $attachment, $upload[‘file’], $post_id );

    require_once(ABSPATH . ‘wp-admin/includes/image.php’);

    $attach_data = wp_generate_attachment_metadata( $attach_id, $upload[‘file’] );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    update_post_meta( $post_id, ‘_thumbnail_id’, $attach_id );

    mysqli_close($con);

    header(‘Location: http://masempresas.cea.es/reempresa/mi-cuenta’);

    }

    Hello @disenorapido,

    I think at this point figuring out the issue you have using the support forum is quite hard. I am willing to help you debug it using some kind of IM.

    Thread Starter ImageLuis Alberto Agea Duran

    (@disenorapido)

    Like what?

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘WP Insert Post’ is closed to new replies.