Debugging Wordpress Loops

By : madphp

Specifically in the buddypress forums loop.

I want to be able to alter the SQL so that it returns only specific forums topics. I want to be able to find where the SQL is executed for a specific loop. I might stumble upon on it, but wonder if there is a good way to trace back / debug any and all loops to find the SQL that is generated.

I'm still struggling to understand loops and tags, actions and hooks. So any tips for debugging would be really helpful.

UPDATE: I tell you what I want to do exactly. I want to be able to return forum topics in buddypress forums that meet certain conditions ie I want to return certain forum topics that contain search terms in that topics posts. What is the best way to add those conditions to the forums loop sql.


By : madphp


If you realy whant to use sql then open up wp-includes/query.php , go to functin get_posts. This is a rather big function where all the magic happens. Do not modify anything here but use it to see witch filters are called and when ( eg. at line 1869 you'll see $search = apply_filters_ref_array('posts_search', array( $search, &$this ) ); , witch means you need an filter on the hook posts_search, and then you can modify the sql query, this is just an example pick the right filters ).

Now , if you're just stating out it would be easyer and a better practice to use query_posts function right before the loop starts. The loop usualy starts in you're theme files ( search for available theme files, where to find them and witch one is called and when ) .

This video can help you solving your question :)
By: admin