WordPress.org

Ready to get started?Download WordPress

Forums

WP Query Post by Post ID (2 posts)

  1. bjarvis2785
    Member
    Posted 10 months ago #

    I'm trying to create a new template for my theme.
    What i want to do is actually only show one result in the template.

    So i need to get post id from the url (which i believe is a standard function), and then pass that post_id in to the query so that the template only shows that one post...

    Can i filter by post id as i can't seem to find anything that says i can?

    Any advice on this?

    Thanks,

    Barry

  2. bcworkz
    Member
    Posted 10 months ago #

    Hello again, I hope this is easier than our last one :)

    Actually there is no standard function to get an ID from an URL directly because an URL can result in many matching IDs. This is the purpose of the query and the loop. Take the parameters delineated in the URL, find all posts that match the parameters, and display them using the "loop".

    If the URL parameters are restrictive enough to only return one post, that's all that will be shown by the "loop", you do not need to do anything special.

    If you already know the ID of the post you wish to display, you could simply have the template do that by having the template retrieve the post object with get_post($id); then display the various elements of the post as needed. You would loose a lot of the typical styling doing this because running the "loop" generates many styling cues.

    You can also alter the query so it will only return that one post by hooking into the 'pre_get_posts' action and changing the query vars as needed. Then the loop will run exactly once, generating the styling cues.

    The trick with this action is it fires before every query for anything, so your callback function needs to identify the requested URL actually relates to it's purpose by examining the current query vars and/or using various template tags like is_home(), is_single(), is_archive(), etc.

    The following code will display a single post with ID=123 for every query, no matter what. You need to add in a conditional so it only is applied as needed.

    add_action('pre_get_posts', 'show_123');
    function show_123($query) {
       unset $query->query_vars; //cancel the current query
       $query->query_vars = array( 'p' => 123 );
       return $query;
    }

    The other thing that will happen is the single template will be used because a single post was returned by the query, unless you do something to change that. For instance, if the post is a custom post type, if a custom post type template exists, it will be used instead of single.

    Another way to get a template to be used for only one post is to create a page based on the template instead of making a post. I'm not sure what you're actually trying to do as far a URLs to reach this template, but I hope this has at least been informative and gives you an idea of what direction to follow.

Reply

You must log in to post.

About this Topic