2

I'm working on ajax script that calls wordpress page and retrieves posts as data.

But I've hit a dead end.

Using: http://codex.wordpress.org/WordPress_Query_Vars

When I directly enter url with query_vars it works and posts get filtered correctly, but when I pass the same using ajax it just lists unfiltered posts. I checked get parameters are getting through, but wp is not catching them.

what should I do?

Code:

http://domain.com/blog/wp-content/themes/wovies-bones/getmovies.php?actor=x And works fine, I get single post that has custom taxonomy actor = x.

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

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&

  • Please link to or provide source of said script. – Rarst Jun 17 '13 at 16:08
  • @Rarst done. :) – user8842 Jun 17 '13 at 16:11
  • what is the value of filters_get? though you may be able to eventually get this to work, it is really the wrong way to do ajax in WordPress. read this and go through the examples. – Milo Jun 17 '13 at 16:22
  • jQuery AJAX to a custom (non-WP) PHP file? How is this WordPress related? – EAMann Jun 17 '13 at 16:24
1

That's not the right way to specify data.

When you're using jQuery AJAX, you specify data as an array or an object when passing it in to the method. In your question, it seems like

'filters_get' === '?actor=x&'

Instead, you should do the following:

filters_get = {
    'actor': 'x'
};

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

jQuery will internally parse the URL to automatically create the query string args for you. From the jQuery documentation, the data parameter is:

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests.

Your format shouldn't have the ? in it, because that will break the string. But it's easier to deal with the encoding if you just pass an object (associative array) instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy