Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am building a plugin which sets up a custom post type with some meta information.

I'd like that information to show up in the loop (in particular the one on the main page).

How can I modify the post-template without writing a theme.

I was looking at home_template but it is deprecated for 3.3

share|improve this question
1  
i believe you can do it with the pre_get_posts hook. but i had some trouble myself with tweaking a tax query in this manner, so i'm interested to see what the answer will be for a meta query. –  helgatheviking Mar 28 '12 at 17:29
    
I use add_action('get_template_part_content','add_stuff'); that way I can at least insert the new fields, though not rewrite the entire template –  Cilvic Mar 29 '12 at 19:35
    
interesting. didn't know about the get_template_part_content hook. i prefer pre_get_posts for modifying the query, b/c then you only run the query 1 time, instead of running a new query later on.... seems more efficient. but again i'm not sure about adding the meta values. –  helgatheviking Mar 29 '12 at 20:19

2 Answers 2

for a simple meta query you can do this:

function wpa_47150( &$query ) {
  if(!is_admin()){
    set_query_var('meta_key', 'test_field');
    set_query_var('meta_value','bacon');
  }
}
add_action('pre_get_posts','wpa_47150');

i wasn't sure it was going to work, but apparently you can set the whole meta_query object this way too

function wpa_47150( &$query ) { 

    if(!is_admin() && is_post_type_archive( 'test_post_type' )){    
        $metaq = ( array ( array(
            'key' => 'test_field',
            'value' => array( 10, 50 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
            ))
        );
        set_query_var('meta_query', $metaq );
    }
}

add_action('pre_get_posts','wpa_47150');

need to adjust the conditionals to your specific case

share|improve this answer
    
Thanks, looks like this will get me in the right direction. Thing is I am not really using the fields as conditionals. All I want is to display their values. Will this work for me? –  Cilvic Mar 31 '12 at 12:07
    
i misunderstood your question i think. you should edit it to be more clear. i thought you wanted to do a meta query, but it sounds like you just want to display meta values. check cilvic's answer then. –  helgatheviking Apr 2 '12 at 13:20
    
thx, can you help answer the issue? How can one add (not filter) the custom fields to the original query so that they can be displayed without re-querying? –  Cilvic Apr 2 '12 at 13:22

To just display the fields set a content filter, only issue might be that custom fields are loaded with one separate query for each post:

function muvice_custom_content($content) {
    if ( 'line' == get_post_type() ) { 
        wp_enqueue_style( 'muvice_stylesheet',plugin_dir_url(__FILE__).'muvice.css' );

        $custom_fields = get_post_custom(get_the_ID());

        $newcontent = '<div class ="part_wrapper">';
        $newcontent .='<div class="part1">'.$custom_fields['part1'][0].'</div>';
        $newcontent .='</div><!-- .part_wrapper -->';

        $original = $content;
        $content = $original.$newcontent;
    }

    return $content;
}
add_filter('the_content', 'muvice_custom_content',1);
share|improve this answer
    
i didn't realize you had answered your own question. i don't get what the problem is... does this not work? –  helgatheviking Apr 2 '12 at 14:30
    
@helgatheviking yes that works, the problem is what you described, this will probably send one extra query for each post, while technically it would be enough to just include the custom columns in the wp_query –  Cilvic Apr 3 '12 at 10:24
    
that's just how get_post_custom() and get_post_meta() work. i doubt they are expensive "queries". –  helgatheviking Apr 4 '12 at 14:38

Your Answer

 
discard

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.