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.