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.

There might be an answer here somewhere, but I'm unable to find it, or recognize it if I did. Here's the situation:

I have a custom post type, 'region', that displays on single-region.php. Region names are North, South, East, West.

I have another custom post type, 'news'. Associated with it is a custom field named 'news_region'. When a news item is created, the author is required to specify a value for news_region (All, N, S, E, W).

What I would like to happen is, when on the North region page, all news items with a news_region value of "All" OR "N" are listed.

FINAL VERSION THAT WORKS (thanks to @milo for putting me on the right track):

// get title of region page for comparison with news_region key
$this_region = get_the_title($post->ID);
// query news items with news_region All or $this_region
$args = array(
    'post_type' => 'news',
    'posts_per_page' => 4,
    'meta_query' => array(
    'relation' => 'OR',
        array(
            'key' => 'news_region',
            'value' => 'All',
            'compare' => 'LIKE'
            ),
        array(
            'key' => 'news_region',
            'value' => $this_region,
            'compare' => 'LIKE'
            )
        )
    );
$newslist = new WP_Query( $args );

NOTE: changed values of news_region to 'All, North Region, East Region, South Region, West Region' to match full region names (same as post title of single-region.php).

share|improve this question
    
What is the code you are using for that "North region" page right now? –  s_ha_dum Feb 5 '13 at 0:41
    
Right now I'm using a standard query which lists all the news items on all the region singles. –  Ray Gulick Feb 5 '13 at 14:35

2 Answers 2

up vote 1 down vote accepted

If I understand you correctly, you want a news_region meta_query IN comparison for All or the first letter of the current region post's title (N, S, E, W)

// get first letter of this region post title
$this_region = substr($post->post_title, 0, 1);
// query news items with news_region All or $this_region
$args = array(
    'post_type' => 'news',
    'meta_query' => array(
        array(
            'key' => 'news_region',
            'value' => array( 'All', $this_region ),
            'compare' => 'IN'
        )
    )
);
$news_posts = new WP_Query( $args );
share|improve this answer
    
No. not trying to compare first letters. Trying to show news items with 'SE' custom field attached (could be more regions later) when on the Southeast Region page, and news items with 'SW' custom field when on Southwest Region page. Can see a clue about how to make your code work using the custom fields assigned to news items, however. Will give it a shot and be back. Thanks. –  Ray Gulick Feb 5 '13 at 14:34
    
I changed the custom field values from initials to the full name of regions. Then, I figured, I could compare them with region names on the region pages (North Region is both the post title of a region page, and a value of a the custom field news_region. Having trouble with it, however. My mod of your code does not seem to grab the region post type title for comparison. Will add code to my original question. –  Ray Gulick Feb 5 '13 at 15:06

@Milo's answer is right if you want to solve the problem with the solution you're proposing , but I think turning "Regions" into a taxonomy makes way more sense. (For a bit of background, read the XY Problem.) This is because the behavior of your Region post type is the exact behavior of a default taxonomy archive page. Taxonomies come with a nicer UI too.

In addition, taxonomy terms have a description field by default. While it's not enabled on every theme, it's pretty easy to add to your taxonomy-region.php template (that assumes you make a taxonomy called region).

Finally, a term description field is plain text by default, but the Rich Text Tags plugin adds a nice WYSIWYG editor to take care of that.

share|improve this answer

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.