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="https://wordpress.stackexchange.com/questions/103270/<?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&

1 Answer
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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *