Title: Query_post Question
Last modified: August 20, 2016

---

# Query_post Question

 *  Resolved [Darkmirror](https://wordpress.org/support/users/darkmirror/)
 * (@darkmirror)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/query_post-question/)
 * I am using query post in my template. I don’t want to create like multiple template
   pages for certain categories. Instead, i want to use the number i put in the 
   post as a category id to show the posts.
 * How can i fetch the number from the post and implement it into my template?
 * Many thanks.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/query_post-question/#post-2471129)
 * This is one way to do what you want.
    - Use a Custom Field (named category-to-show, or similar) to hold the category
      id number.
    - Write a template to get the Custom Field and use the category number in a 
      call to query_posts().
    - Create a Page for each category, put the category id in the custom field and
      assign the custom template.
 *  Thread Starter [Darkmirror](https://wordpress.org/support/users/darkmirror/)
 * (@darkmirror)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/query_post-question/#post-2471131)
 * How can i get the value from the custom field in the template. Is there any specific
   code that i need to implement?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/query_post-question/#post-2471134)
 * You use the [get_post_meta() function](https://wordpress.org/support/topic/query_post-question/?output_format=md).
   Your code would look something like this:
 *     ```
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $cat_id = get_post_meta($post->ID,'category-to-show',true);
       if (intval($cat_id)) :
          query_posts("cat=$cat_id&paged=$paged");
          if (have_posts()) : while (have_posts()) : the_post();
             // your code to display posts
             endwhile;
          else :
             // code for no posts in category
          endif;
       else :
          // code for no category in custom field
       endif;
       ```
   
 *  Thread Starter [Darkmirror](https://wordpress.org/support/users/darkmirror/)
 * (@darkmirror)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/query_post-question/#post-2471136)
 * Thank you very much!… I’ve been waiting for someone to help me..^.^ I am a high
   school student and a noob to php and wordpress haha… Coding is very fun!
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/query_post-question/#post-2471141)
 * Good luck with it!!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Query_post Question’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/query_post-question/#post-2471141)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
