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: </label><select 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'</select><br />';
echo "<label for=\"mae\">Mãe: </label><select 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'</select><br />';
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.
return
content, notecho
.wp_reset_postdata
restores the global$post
,wp_reset_query
restores the global$wp_query
.get_posts
alters neither of those vars on its own, your problem is elsewhere.var_dump($wp_query)
on those requests and look at what it contains.var_dump($wp_query)
gave me a clue of what's happening, and that looks really weird now. The $_POST array from the form is loaded as $wp_query->query_vars when the form is submitted. The same thing happens with the$args
array fromget_posts()
. Since one of the$args
key is post_type WordPress try to load the page_id where the form is but with the post_type that was used inget_posts()
, instead of 'page' post_type. That's why it outputs "Not found". When I comment outget_posts()
it works, because there's no post_type in the query _vars. Any idea why is that happening?post_type
with your form, it is getting added by WordPress because you are sending a form element namedmae
, which is a post type. WordPress thinks you are trying to query for a post of typemae
with slug that matches whatever is submitted with your form. That's why you can't use any form element names that conflict with query vars.