The correct answer is to just do AJAX the right way.
Enqueue your script and localize it to add the path to admin-ajax.php
:
function wpd209588(){
wp_enqueue_script(
'wpd209588_script',
get_template_directory_uri() . '/js/your-script.js?ver=1.0',
array( 'jquery' )
);
wp_localize_script(
'wpd209588_script',
'WPaAjax',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
)
);
}
add_action( 'wp_enqueue_scripts', 'wpd209588_scripts' );
In your-script.js
, do your AJAX call:
jQuery(document).ready(function($){
$.post(
WPaAjax.ajaxurl,
{
action : 'wpd209588_ajax'
},
function( response ){
$( '.container' ).html( response );
}
);
});
Map your AJAX handler to the action you call in your AJAX script:
function wpd209588_ajax_function(){
global $wp_query;
$args = array(
'posts_per_page' => -1
);
$wp_query = new WP_Query( $args );
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
exit;
}
add_action( 'wp_ajax_wpd209588_ajax', 'wpd209588_ajax_function' );
add_action( 'wp_ajax_nopriv_wpd209588_ajax', 'wpd209588_ajax_function' );