0

Preamble

In my functions.php file I added a custom shortcode. This shortcode takes a query as a parameter and queries the wordpress database. From there it takes the posts content and returns it. This shortcode is then used on a non-custom page.

The issue

Using the shortcode on any page with a given and correct query, the shortcode will display the properly formatted content, but twice. This is although the query only returns one post.

So let's say I had [query_post query='post_type="post"'] on a page. The shortcode returns all posts of the type post (which in my example is just one). The content is displayed fine, but at the end of the page the already displayed content repeats again. Why is this?

The code

In the functions.php I appended this:

function query_post_shortcode_function( $atts ) {   
    // Argument parsing
    $arguments = shortcode_atts( array(
        'query' => "",
    ), $atts );

    // Query
    $posts = query_posts( $arguments['query'] );

    // Getting the content for each queried post
    unset($content);
    foreach ($posts as $post) {
        $content .= apply_filters( 'the_content', $post->post_content );
    }

    return wpautop($content);
}   
add_shortcode( 'query_post', 'query_post_shortcode_function');

Which, as I've stated already, will query the wordpress database for a post and then return its content.

Steps taken already

  • Instead of appending the content from all posts, I tried just assigning it. So $content = apply_filters(...) insted of .=. This did not change the result
  • Echoing something inbetween will show in the first iteration of the returned content, but not in the second one
1

Solution attempt

Due to @AlexanderHolsgrove's comment, I tried reutrning a simple string, so instead of return wpautop($content); I tried return "test return";. This outputted the string on top of the queried post content. Which is weird, because I do not echo nor do I return the post content in any way now.
So I tried leaving out the echo entirely, and it still displayed the post content. Now @MikeNGarret's and @mrben522's answer comes into play. After reading through the SO answer and the corresponding WordPress Codex entry, I found out that query_posts() actually kind of replaces the post you are running the query on (it's all a bit confusing, read through the codex or view the answer to get some more detail). This finally left me with two answers:

Solution

The not suggested way is to just use query_posts(). It displayed the content fine in my circumstances, but from what I understand, it is not optimal at all and should not be used.

The real solution is to use WP_Query or rather an isntance of it. Basically I rewrote the whole lower section of my code to achieve this:

function query_post_shortcode_function( $atts ) {   
    // Argument parsing
    $arguments = shortcode_atts( array(
        'query' => "",
    ), $atts );

    // Query
    $wpQuery = new WP_Query( $arguments['query'] );

    // The Loop
    if ( $wpQuery->have_posts() ) {

        // Capture output
        ob_start();
        while ( $wpQuery->have_posts() ) {
            $wpQuery->the_post();
            echo apply_filters( 'the_content', get_the_content() );
        }

        // Return output
        return ob_get_clean();
    }
}   
add_shortcode( 'query_post', 'query_post_shortcode_function');

I create a new instance of the WP_Query and give my single argument to it. Then I check if the created instance has posts and if there are, I loop through them echoing all output with the applied 'the content' filter. This output is captured by ob_start() and then returned by ob_get_clean().

So far it has worked flawlessly and did exactly what I wanted to achieve. I hope others can use this information as well and that my self-written answer is correct and applicable to not just me.

  • I'll not mark mine as the accepted answer yet, because someone else might just have a better way of explaining the things I wrote – totgezockt Apr 9 at 12:27
  • Looking back at your initial code, I wonder if you actually needed to use wp_reset_query, just before the return statement. – Alexander Holsgrove Apr 10 at 14: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.