Query vars in url (wordpress) work fine when page loaded directly, but fail on ajax

I’ve a little issue, and I couldn’t find anything about it… I’m in deadlock.

So I’ve wp page that contains normal loop, if I call this file directly in browser all works fine, and I can even use query-vars such as: http://domain.com/blog/wp-content/themes/wovies-bones/getmovies.php?actor=x And it works fine, I get single post that has custom taxonomy actor = x.

Read More

But when I try to load same using ajax, it behaves differently and ignoring my get request returns all posts…

Any ideas what is happening?

ajax-jquery part:

$.ajax({
  url: templatePath + "getmovies.php",
  type: "GET",
  data: filters_get,
  cache: false,
  success: function (data){
    console.dir(data);
  }
});

And php part:

    /* Define these, So that WP functions work inside this file */
    define('WP_USE_THEMES', false);
    require('../../../wp-blog-header.php');


?>






    <div id="container">
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div class="movie">
            <a href="<?php the_permalink() ?>">
                <?php
                    if (has_post_thumbnail()) {the_post_thumbnail('homepage-preview');}
                    else {echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/default-poster.jpg" />';}
                ?>
                <p class="comments"><?php comments_number('0 review','1 review','% reviews'); ?></p>
                <div class="description">
                    <h2><?php the_title(); ?></h2>
                    <?php the_excerpt(); ?>
                </div>
            </a>
        </div>
        <?php endwhile; else: ?>
            <!-- No movies found -->
        <?php endif; ?>
    </div><!-- End #container -->

filters_get looks like this: ?actor=x&

Related posts

Leave a Reply

1 comment

  1. This way, you just load WordPress and run your own PHP file. Basicaly, I think you should redefine your WP_Query like this :

    /*Define these, So that WP functions work inside this file */
    define('WP_USE_THEMES', false);
    require('../../../wp-blog-header.php');
    
    global $wp_query;
    
    $args = array(
        'post_type' => 'my-post-type',
        'my_tax'    => $_GET['my_tax']
    );
    
    $wp_query = new WP_Query($args);
    
    // Your template stuff
    

    And if you’re using AJAX to get your posts, you’d better use a hooked function as it is described in WordPress Codex :

    add_action('wp_ajax_get_my_cpt', 'get_my_cpt');
    add_action('wp_ajax_nopriv_get_my_cptn', 'get_my_cpt');
    
    function get_my_cpt() {
        $args = array(
            'post_type' => 'my-post-type',
            'my_tax'    => $_GET['my_tax']
        );
    
        $cpts = get_posts($args);
    
        if(empty($cpts)) {
            _e('No custom post to show');
            exit;
        }
    
        foreach($cpts as $cpt) {
            // Do you stuff to display your custom posts
        }
    
    }
    

    Then you will have to use this kind of url in your AJAX call : http://www.domain.tld/wp-admin/admin-ajax.php?action=get_my_cpt&my_tax=keyword