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.

Working on a website and need loop that will show posts ONLY if custom field value is equal to the current category name.

I have category 'fun' and custom field 'genre'. I would like to show on custom archive page only posts that are selected to be under category 'fun' AND at the same time if the value in custom field 'genre' is 'fun'. Order I prefer is most visited posts first at the top - if possible.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Something like this should be the query you want to run, it adds a condition to the page query that requires the value in "meta_field" to equal the current category's title.

global $wp_query;
$args = array('meta_query' => array(
    array(
        'key' => 'meta_field',
        'value' => single_cat_title("", false),
        'compare' => '='
    )
  )
);

query_posts(array_merge($wp_query->query, $args));
// The Loop Goes Here //
share|improve this answer
    
Thanks Dave, but I get Parse error: syntax error, unexpected ';', expecting ')' At the last few lines, I can't figure out why... –  Dean Sep 9 '12 at 9:42
    
global $wp_query; $args = array('meta_query' => array( array( 'key' => 'meta_field', 'value' => single_cat_title("", false), 'compare' => '=' ) ) ); query_posts(array_merge($wp_query->query, $args)); // The Loop Goes Here // here you go... one ")" was missing –  Daniel Sachs Sep 9 '12 at 10:41
1  
Thanks for catching that, I've updated my answer with the correction –  Dave Hunt Sep 10 '12 at 1:03
    
Thanks a lot Daniel, I said I am noob :) Thanks Dave too, this code works perfectly. –  Dean Sep 10 '12 at 1:56
    
Hey Dave & Daniel, how would I add ('orderby' => 'rand') to this query? Or even better order by popular posts first? –  Dean Sep 10 '12 at 8:55
show 1 more comment

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.