Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to reload the fresh posts using jQuery. As far as I know, I can't reload the contents of a div inside the page so I reload a file into that div.

The problem is that my loaded file gives me a Fatal error: Call to undefined function wp_query()

How can I implement functions to a newly created file inside the theme directory to work properly?

This is my jQuery

<script language="JavaScript">
$(function() {
    var SANAjax = function(){

    }
    setInterval(SANAjax, 15000 );
});
</script>

And this is what i have in reloadhomeposts.php (I have deleted the content though)

<?php $recent = new WP_Query("cat=3,4,5&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
.
.
.
<?php endwhile; ?>
share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

The reason for this error is that your loading the file without loading in the WordPress system and so wp_query() doesnt exist.

Quick fix is to:

include("../../../wp-load.php"); <-- guessing at the location of the wordpress file.

at the top of the php file.

share|improve this answer
    
Hi Brady, I will check it soon and tell you. So wp-load loads the functions used in wordpress? –  Punkis Apr 18 '11 at 11:41
    
Working! How can I load the plugins too ? –  Punkis Apr 18 '11 at 11:50
1  
Yes this loads wordpress. But you need to come up with a way to check if Wordpress is already loaded or not. For example if this is a page template then on normal load circumstances wordpress is already loaded, but not loaded when done via an ajax call. –  Brady Apr 18 '11 at 11:50
    
as for the plugins that I'm not too sure. Maybe someone else can help here. If you are dealing with a template page then maybe in your ajax call it would be better loading like this: example.com/mypage then use jquery to just grab the html you need to update. this way everything is loaded that you are going to need –  Brady Apr 18 '11 at 11:52
add comment

The correct way to handle this is to use WP's admin-ajax.php file, instead of your own. See this Codex page:

http://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side

Instead of putting the code in a plugin, just put it in your functions.php file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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