Title: wp_query with custom fields
Last modified: August 20, 2016

---

# wp_query with custom fields

 *  [goldmember](https://wordpress.org/support/users/goldmember/)
 * (@goldmember)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/wp_query-with-custom-fields/)
 * i want to create a list of posts from category XYZ, and sort them alphabetically
   based on what’s contained in the VALUE field of a custom field called “studentname“.
 * i tried this code and it didnt work. any idea what’s wrong? thanks in advance.
 *     ```
       <?php
   
         	$loop = new WP_Query( array(
           		'showposts' => 5,
           		'post_type' => 'studentname',
           		'category_name' => XYZ,
       		'orderby' => 'meta_value',
       		'meta_key' => 'studentname'
           		)
           		);
         	while ( $loop->have_posts() ) : $loop->the_post(); ?>
          	<div>
             		<h2><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
           	</div>
         <?php endwhile; ?>
       ```
   

Viewing 1 replies (of 1 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/wp_query-with-custom-fields/#post-2893226)
 * The value for the ‘category_name’ parameter needs to be in quotes:
 *     ```
       'category_name' => 'XYZ',
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘wp_query with custom fields’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [14 years, 1 month ago](https://wordpress.org/support/topic/wp_query-with-custom-fields/#post-2893226)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
