E
E
EvgenyApMr2021-07-05 19:10:06
AJAX
EvgenyApMr, 2021-07-05 19:10:06

Why doesn't asynchronous product display work in WordPress?

Greetings.

There is such a script that connects the php file on click.

$('#form_help').on('submit',function(e){  
 e.preventDefault();
 var self = this;
       $.ajax({
            url: '/view_test.php',
            type: "POST",
            cache: false,
            data: $(this).serialize(),
            success: function(data) {
               $('.message').html(data);
            }
          });
});


In this file, I need to enumerate the id of the goods and display them. Here is an example code:

$args = array(
 'post__in' => array(1, 7, 3869), 
 'post_type' => 'product',
 );
$query = new WP_Query( $args );
 if ( $query->have_posts() ) {
 while ( $query->have_posts() ) {
 $query->the_post();
 echo '<li>' . get_the_title() . '</li>';
 }
 } else {

 }
 wp_reset_postdata();


Separately, each code works, but together - no. I suspect that WP_Query does not work through asynchronous loading.

Tell me, is there any way to make the second script work on asynchronous loading?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Neverov, 2021-07-05
@TTATPuOT

See what the browser console writes when requesting backing. Most likely, a request error, if the back is guaranteed to respond.
Whatever your load is, the backend doesn’t care at all. For him, your request from JS is the same request as hundreds of others.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question