1

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.

1 Answer 1

2

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 //
6
  • 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, 2012 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 Sep 9, 2012 at 10:41
  • 1
    Thanks for catching that, I've updated my answer with the correction
    – Dave Hunt
    Sep 10, 2012 at 1:03
  • Thanks a lot Daniel, I said I am noob :) Thanks Dave too, this code works perfectly.
    – Dean
    Sep 10, 2012 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, 2012 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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