Title: using query_posts() and custom_fields.
Last modified: August 21, 2016

---

# using query_posts() and custom_fields.

 *  [calprinicus](https://wordpress.org/support/users/calprinicus/)
 * (@calprinicus)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/using-query_posts-and-custom_fields/)
 * I am new to wordpress but not new to scripting and code.
    I need to get the value
   from a page’s custom field into a query_posts() of a page template.
 * I needed a way to add posts to a static page so I created a custom page template
   based off of page.php. it’s called articles-page.php. In this custom template
   I added the following code AFTER the loop that appears to print the contents 
   of the page.
 *     ```
       <?php
       global $more;
       $more = 0;
       query_posts('cat=5');
       if(have_posts()) : while(have_posts()) :the_post(); ?>
       <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
       <div><?php the_content('Read More') ?></div>
       <?php
       endwhile;
       endif;
       wp_reset_query();?>
       ```
   
 * This is currently working. I have my page’s content and it is followed by posts
   that have ‘cat=5’. However I want versatility so I can apply this custom template
   to multiple pages (each with different $args i want to pass to query_posts()).
   Inside each page is a “custom fields” tab. For each page I have a custom field
   who’s name is “query_args” and their value is a string/array I want to pass to
   the query_posts() in the code above.
 * What would I need to replace the ‘cat=5’ with in my code above, so that it will
   read the value of ‘query_args’ from the custom field that is defined for that
   page?
 * Website: (News Page) [http://www.TheChosenRPG.com](http://www.thechosenrpg.com)
   
   I am using wordpress 3.5.1. with sliding door theme

Viewing 1 replies (of 1 total)

 *  Thread Starter [calprinicus](https://wordpress.org/support/users/calprinicus/)
 * (@calprinicus)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/using-query_posts-and-custom_fields/#post-3824226)
 * I solved this one myself after lots of trial and error.
    The line needed to be
   changed to:
 *     ```
       query_posts(get_post_meta($post->ID, 'query_args', true));
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘using query_posts() and custom_fields.’ is closed to new replies.

## Tags

 * [custom_field](https://wordpress.org/support/topic-tag/custom_field/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [calprinicus](https://wordpress.org/support/users/calprinicus/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/using-query_posts-and-custom_fields/#post-3824226)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
