Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Looking for a way to include custom fields in the output of WP_Query. I am managing a site where nearly every post type has a lot of custom fields defined and it would be much nicer not to have to run get_post_custom in almost every loop.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You may want to hook somewhere in the gazillion hooks available in wp-includes/query.php.

I would suggest something like this:

function my_always_get_post_custom( $posts ) {

    for ( $i = 0; $i < count($posts); $i++ ) {

        $custom_fields = get_post_custom( $posts[$i]->ID );
        $posts[$i]->custom_fields = $custom_fields;


    return $posts;


add_filter( 'the_posts', 'my_always_get_post_custom' );

In this way you will always have your posts custom fields at hand in your $post object without having to bother to look for them every time you are setting up a loop. From now on, you can just access them using $post->custom_fields as a multi-dimensional array.

share|improve this answer

WP_Query let you query posts based on key or values from the custom fields if you would like to get multiple values from custom fields use wpdb you can read more about here.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.