I would like to alter the main wp_query and then email the results as PDF or HTML email.

I have managed to filter wp_query using custom filters and some code similar to this inside my plugins function:

add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
    if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
    $query->set( 'cat', '-5' );

    // CALL preparePDF($wp_query) here in order to prepare the PDF

The results are successfully filtered and presented to the user.

At this point (or even before presenting the results to the user I would like to do the following (I am actually trying to do this inside the same function I used to filter the main query:

I created a new function where I pass the main wp_query as parameter: and I try to print the results:

function preparePDF($wp_query) {


// The Loop
if ( $wp_query->have_posts() ) {
    echo '<ul>';
    while ( $wp_query->have_posts() ) {
        echo '<li>' . get_the_title() . '</li>';
    echo '</ul>';
} else {
    // no posts found
    echo 'no posts found';


Unfortunately even if my query seems correct when I use var_dump, I get no results.

Can I somehow fix this? Am I doing it the wrong way?

1 Answer 1


It looks like you're calling preparePDF( $wp_query ) inside your callback, but $wp_query isn't defined and the global $wp_query shouldn't be accessible there without global $wp_query.

Try instead preparePDF( $query ) and use wp_reset_postsdata() to restore the global $post object.

Another important thing is that you must run your preparePDF() function after WP_Query has fetched the posts from the database. So don't use the pre_get_posts hook, use a later hook instead. But that hook depends on what you want to do with the posts. I also don't understand what you're preparing with your preparePDF() function.

Remember to use WP_DEBUG on your dev install, to catch any PHP errors, warnings and notices. Check for example the Debugging in WordPress in the Codex.

  • Do you believe is it better to use the_posts filter to get the results and prepare the pdf from there instead of calling the wp_query?
    – glarkou
    May 14, 2015 at 11:55
  • Btw I am not calling the global $wp_query. I am using $query which is passed by the callback and I correctly modify it. Why I am not able to call it? I mean why I am not able to use $query->have_posts().
    – glarkou
    May 14, 2015 at 12:05
  • yes, no posts have been fetched within the pre_get_posts callback, we must use a later hook. But I'm not sure I undertand what you're preparing with preparePDF(), what are you modifying there? @glarkou
    – birgire
    May 14, 2015 at 15:54
  • I am getting the posts and I prepare a PDF similar to what the result is in the Wordpress admin. Afterwards I email these results to the user.
    – glarkou
    May 14, 2015 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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