wp_reset_postdata() and wp_reset_query() inside shortcode are not working to reset original page query

I'm implementing a form for a frontend post with a shortcode. The form is processed in the same function, reloading the page:

function adicionar_ninhada(){
$user_id = get_current_user_id();
if (isset($_POST['adicionar_cachorro'])){
    wp_verify_nonce( $_POST['_wpnonce'],  'brg_add_dog'.$user_id );
    $args = array(
      'post_title'    = $_POST['raca'] . " nascidos em " . $_POST['nascimento'] ,
      'post_content'  = '',
      'post_status'   = 'publish',
      'post_author'   = $user_id,
      'post_type'   = 'ninhada',
    );
    $post_id = wp_insert_post( $args );
    if(isset($_FILES['foto_upload'])){
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
        $attachment_id = media_handle_upload( 'foto_upload', $post_id );
        if ( is_wp_error( $attachment_id ) ) {
            echo "erro na imagem";
        } else {
            update_post_meta($post_id, '_thumbnail_id', $attachment_id);
        }
    }
    update_post_meta($post_id, 'raca', $_POST['raca']);
    update_post_meta($post_id, 'machos', $_POST['machos']);
    update_post_meta($post_id, 'femeas', $_POST['femeas']);
    update_post_meta($post_id, 'data', $_POST['nascimento']);
    update_post_meta($post_id, 'pai', $_POST['pai']);
    update_post_meta($post_id, 'mae', $_POST['mae']);
    echo "Adicionado com sucesso!";
}else{
    echo "form enctype=\"multipart/form-data\" method=\"post\"
    Raça: "; 
    wp_dropdown_categories(
        array(
            'taxonomy' = 'raca', 
            'hide_empty' = 1,
            'show_option_none'   = 'SRD',
            'option_none_value'  = 'Outro',
            'name' = 'raca',
        )
    );
echo "br /
    Machos: input type=\"number\" name=\"machos\" /br /
    Fêmeas: input type=\"number\" name=\"femeas\" /br /
    Data de nascimento: input type=\"text\" name=\"nascimento\" class=\"brgdatepicker\"/br /
    label for=\"pai\"Pai: /labelselect name=\"pai\"";
    $args = array( 'post_type' = 'pai', 'posts_per_page' = -1, 'post_status' = 'published', 'post_parent' = null); 
    $pais = get_posts( $args );
    foreach ($pais as $pai){
            echo 'option value="'.$pai-ID.'"' . get_the_title($pai-ID) . '/option';
        }
        echo'/selectbr /';
    echo "label for=\"mae\"Mãe: /labelselect name=\"mae\"";
    $args = array( 'post_type' = 'mae', 'posts_per_page' = -1, 'post_status' = 'published', 'post_parent' = null); 
    $maes = get_posts( $args );
    foreach ($maes as $mae){
            echo 'option value="'.$mae-ID.'"' . get_the_title($mae-ID) . '/option';
        }
        echo'/selectbr /';
    echo "
    input type=\"hidden\" name=\"adicionar_cachorro\" value=\"1\"
    Foto: input type=\"file\" name=\"foto_upload\" id=\"foto_upload\"  multiple=\"false\" /br /
    button type=\"submit\" name=\"enviar\"Salvar/button
    ";
    wp_nonce_field( 'brg_add_dog'.$user_id );
    echo "/form";
wp_reset_query();
}
}
add_shortcode('adicionar_ninhada', 'adicionar_ninhada');

I've tried already wp_reset_postdata() after each get_posts() foreach loop , wp_reset_postdata() in the end of the function and both scenarios with wp_reset_query() too. But the original page query is always broken after form submission and it says "Nothing found". If I comment out the get_posts() everything works fine.

Topic wp-reset-query wp-reset-postdata shortcode wp-query custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.