Loop to display random posts only if a custom field matches category

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.

Related posts

Leave a Reply

1 comment

  1. 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 //