Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to run wp_query on a separate php file for an ajax call

Tags:

php

wordpress

For example:

<?php $numposts = $_POST['showposts']; ?>


<?php $home_query_bottom = new WP_Query("cat=&showposts=$num_posts&offset=5"); $b = 0; ?>
<ul class="thumbs">
    <?php while ($home_query_bottom->have_posts()) : $home_query_bottom->the_post();
        $do_not_duplicate = $post->ID; $b++; ?>

        <li class="post-<?php the_ID(); ?> thumb"><?php get_the_image( array( 'custom_key' => array( 'thumbnail' ), 'default_size' => 'thumbnail', 'width' => '160', 'height' => '160' ) ); ?></li>
    <?php endwhile; wp_reset_query(); $b = 0; ?>
</ul>

The code above is on its own php file ready to be called by the main wordpress page, however I get an error saying wp_query class not found. I'm assuming it's because I am not using the header.html which probably has a bunch of includes. What do I need for that page to utilize the wp_query class?

like image 590
Adam Avatar asked Jan 09 '11 20:01

Adam


2 Answers

You can turn the template engine off and then include the header.
// Include WordPress
define('WP_USE_THEMES', false);
require_once('PATHHERE/wp-blog-header.php');

like image 77
MrGlass Avatar answered Sep 28 '22 00:09

MrGlass


You have to include the file that has the Wordpress functions located on the main directory of the Wordpress Installation:

define('WP_USE_THEMES', false);  
require_once('../../../wp-load.php');

../../../ = path to the main directory on your installation

I found a nice tutorial about this here.

like image 28
Gastón Labarthe Avatar answered Sep 28 '22 00:09

Gastón Labarthe