0

I would like to insert a wp_query after the post content. After looking in different website, they all suggest using a filter on the_content. Example: add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );

function add_query_after_content( $content ) {

        echo $content;
        echo *** The query ****;
    }

add_filter( 'the_content', 'add_query_after_content' ) 

However it creates an infinite loop (the content in the query is filtered as well).

What is a good way to entering the query automatically after the post, using a plugin that will work on most themes ?

0

You should remove your hook before query.

function wpse_288691_add_query_after_content( $content ) {

    $ouptut = '';

    remove_filter( 'the_content', 'wpse_288691_add_query_after_content' );

    // Do your *** WP_Query ***  and assign result to $ouptut.
    // Reset query by executing `wp_reset_query`.

    add_filter( 'the_content', 'wpse_288691_add_query_after_content' );

    return $content . $ouptut;
}

add_filter( 'the_content', 'wpse_288691_add_query_after_content' );

Do not echo anything inside the filter, only return values.

  • it was not the add/remove filter that solve my problem, but using return insteat of echo – Jeremaya456853 Dec 15 '17 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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