Title: Insertin custom field in php code?
Last modified: August 19, 2016

---

# Insertin custom field in php code?

 *  [zefdesign](https://wordpress.org/support/users/zefdesign/)
 * (@zefdesign)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/)
 * I’m using this code to display a loop within the main loop:
 *     ```
       <?php $my_query = new WP_Query('category_name=<strong>HERE-IS-WHERE-I-WANT-TO-INSERT-CUSTOM-FIELD</strong>&orderby=title&order=asc&showposts=100');while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate=$post->ID;?>
   
       <li><?php the_title();?></li>
        <?php endwhile; ?>
       ```
   
 * How could I insert a custom field within that php code???
    I tried this:
 *     ```
       <?php
       $post_title = get_post_meta($post->ID, 'post_title', $single = true);
       ?>
       ```
   
 * then inserting `$post_title.` within the code.
 * Didn’t work. There must be a way?

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

 *  Thread Starter [zefdesign](https://wordpress.org/support/users/zefdesign/)
 * (@zefdesign)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163236)
 * I’m sure this is doable but I can’t find anything saying how…
 *  Thread Starter [zefdesign](https://wordpress.org/support/users/zefdesign/)
 * (@zefdesign)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163394)
 * PLease help monday people!!!!!
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163395)
 * I personally prefer to use this method..
 *     ```
       $my_query = new WP_Query();
       $my_query->query('orderby=title&order=asc&showposts=100');
       ```
   
 * Preferences aside..
 * All you need to do is add the meta parameters to the above query, it works the
   same as a regular query_posts, so just add what meta parameters you need.
 * See.
    [http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters](http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters)
 *  Thread Starter [zefdesign](https://wordpress.org/support/users/zefdesign/)
 * (@zefdesign)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163398)
 * Thanks for responding, I am very bad at php however and still don’t know how 
   to apply the code. I tried using it but it wasn’t working for me. the code I 
   posted does work… but I don’t know hwo to stick my custom field in the code.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163401)
 * Click the link i gave you above, the parameters are covered on that page, all
   you need to do is add them in with your other parameters..
 * example..
    `('order=asc&meta_key=SOMEFIELNAME&meta_value=SOMEVALUE')`
 * The link provides ample examples.
 * Consider `$my_query->query();` as the equivalent of `query_posts();`
 * So for example..
    `query_posts('showposts=1');` in the custom query would be..`
   $my_query->query('showposts=1');`
 *  Thread Starter [zefdesign](https://wordpress.org/support/users/zefdesign/)
 * (@zefdesign)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163402)
 * Maybe I didn’t explain what I mean clearly enough.
 * I have a post for an album for example it’s called ‘Apples’. That post has a 
   custom field called post title which I assing the vale ‘Apples’.
 * I then have my code in single.php. And I want it to display a list of posts from
   the category name of ‘Apples’. But i want to assing the category name using my
   custom field.
 * eg:
 * <?php $my_query = new WP_Query(‘category_name=INSER CUSTOM FIELD HERE
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163404)
 * Ok, you mean something like this?
 *     ```
       <?php
       $fieldname = 'Apples';
       $field = get_post_meta( $post->ID , $fieldname , true );
       if(!$field) echo $fieldname . ' missing for this post' ; return;
   
       $my_query = new WP_Query();
       $my_query->query('category_name='.$field.'&orderby=title&order=asc&showposts=100');
   
       	while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate=$post->ID;
       	?>
       		<li><?php the_title();?></li>
       	<?php
       	endwhile;
       	?>
       ```
   
 *  Thread Starter [zefdesign](https://wordpress.org/support/users/zefdesign/)
 * (@zefdesign)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163408)
 * That looks exactly like the right sort of thing actually, thanks. I tried it 
   and it hasn’t generated anything.
 * The code I would normally use would be: `<?php $values = get_post_custom_values("
   post_title"); echo $values[0]; ?>` But obviously I can’t just put that in the
   code…
 * Sorry about this, but i’m real grateful
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163409)
 * $field will be the value of the custom field…
 * So the code i posted will look for a category with the name of whatever you placed
   in the custom field called “Apples” per the example..

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

The topic ‘Insertin custom field in php code?’ is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [field](https://wordpress.org/support/topic-tag/field/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [16 years, 9 months ago](https://wordpress.org/support/topic/insertin-custom-field-in-php-code/#post-1163409)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
