I'm trying to make a AJAX request on my theme.

I already know wordpress handle AJAX very well, but not directly on theme file. It uses admin-ajax.php for this.

I working on this for a long time and really don't know where to put the code.

I know there need to put information in admin-ajax.php, function.php and have to specify if user is ou not logged in.

Can someone help me with my code?


<article class="delivery-individual">
        <!-- # CONTENT HERE -->


      url: "<?php bloginfo('template_directory'); ?>/temakis.php",
      dataType: "html",
      success: function(result) {



$args = array(
    'post_type'      => 'post',
    'category_name' => 'temakis');
while (have_posts()) : the_post();

<div class="item-individual">
  <img src="<?php the_field("imagem-produto");?>" width="150" height="150" title="<?php echo get_the_title(); ?> "/>
  <div class="item-individual-info">
  <h4><?php echo get_the_title(); ?></h4>
  <p class="descricao-delivery"><? echo get_post_field('post_content'); ?></p>
  <p class="valor-delivery">R$ <? the_field("preco"); ?></p>

<?php endwhile; ?>
<?php wp_reset_query(); ?>

The request is ok, because i`m getting the h3 correctly.

Can anyone help me?



2 Answers 2


You need to set user logged in condition on button/ link which triggers ajax request, eg, if user is not logged in open login modal else your function.


It doesn't work because your external file is not loaded under wordpress, and therefore doesn't have access to any wordpress functions.

As soon as your external file hits query_posts() it will throw a fatal error as the function is not defined.

You should use the Ajax API as has been suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.