Skip to main content
1 of 2

How to write a query-function as a query-shortcode?

I ask this question as a follow-up to this one.

I have this query-function in functions.php but AAMOF I don't want to change functions.php at all (besides for CPTs).

I'd like to print the same data directly from WP GUI via shortcode.

How should a functions.php query-function should be translated into a query-shortcode?

$posts = get_posts(array(
    "post_type"=>"book_pages",
    "post_status"=>"publish",
    "posts_per_page"=>-1 
));

if ($posts && count($posts)>0) 
{
    global $post;
    foreach ($posts as $post)
    {
        setup_postdata($post);
        the_title(); //prints out the title of this post
    }
    wp_reset_postdata(); 
}