Quantcast
Channel: Question and Answer » meta-query
Viewing all articles
Browse latest Browse all 36

How can I include posts with a certain meta value into a taxonomy archive page?

$
0
0

So for example, say I have a taxonomy “Blogs” and term “News” as well as other terms like “Food”, “Sports”, …etc.

On the “News” archive page, you obviously only get posts that include term “News”, but what I would like to do is add a metabox on all posts to optionally allow an author to include a post from say “Sports” into the “News” archive page.

NOTE simply categorizing a post as both its normal term and “News” doesn’t work because we do some unique things with our permalinks that this method sometimes screws up.

I assume this would involve modifying the query with pre_get_posts() but I’m not sure how to make the meta_query optional so that posts with the term “News” don’t have to check the “Include in News blog” metabox to remain in their archive.

Here is my current attempt, but it doesn’t really come close to working…

function news_section_include_others( $query ) {
    if ( $query->is_main_query() && !is_admin() && is_tax( 'pm-blogs','news' ) ) {
        $meta_query = $query->get('meta_query');
        $meta_query[] = array(
                'key'=>'pm_include_in_news',
                'value'=>'Yes',
                'compare'=>'OR',
            );
        $query->set('meta_query',$meta_query);
    }
}

Viewing all articles
Browse latest Browse all 36

Trending Articles