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.

I have installed the more fields plugin which gives me access to a custom field checkbox. When setting the field up through the plugin the caption says: Creates single checkbox, which key is either on/off. As you can see by the query I am looking for all the events whose event-archive = on.

Is there a better way to do this? This doesn't return any posts anyways. Is there a better way to build a boolean field?

$archive_query = new WP_Query(
        array( 
          'post_type'  => 'event',        // only query events
          'meta_key'    => 'event-archive',  // load up the event_date meta
          'order'       => 'desc',         // ascending, so earlier events first
          'posts_per_page' => '5',
          'meta_query'  => array(
             array(         // restrict posts based on meta values
              'key'     => 'event-archive',  // which meta to query
              'value'   => 'on',  // value for comparison
              'compare' => '=',          // method of comparison
              'type'    => 'CHAR'         // datatype, we don't want to compare the string values
             ) // meta_query is an array of query ites
            ) // end meta_query array
          ) // end array
        ); // close WP_Query constructor call
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Have you already looked at the output of WP_Query, if you don't submit the "meta_query" key? Do you get the posts for the event-posttype?

If yes, then you could check what the "more fileds"-plugin puts in the "event-archive" custom field. Maybe you have to set the value to integer 1 or just submit a boolean true. e.g.

array(
'key'     => 'event-archive', 
'value'   => 1
)

array(
'key'     => 'event-archive', 
'value'   => true
)

EDIT

I've just installed the plugin, created a new meta field "event-archive" of type "checkbox" and some posts with and without this meta value.

If I use the following query, I get all posts from posttype "event" with the metavalue selected.

$args = array(
'post_type' => 'event',
'order' => 'desc',
'posts_per_page' => '5',
'numberposts' => -1,
'meta_query' => array(
    array(
        'key' => 'event-archive',
        'value' => 1,
        'compare' => '=',
        'type' => 'CHAR'
    )
)

$archive_posts = get_posts($args);
print_r($archive_posts);

Note

  • You can also use WP_Query if you like
  • The "meta_query" parameter requires WordPress 3.1
share|improve this answer
    
I can get the events post type if I remove the meta_query. The More-Fields plugin checkbox is returning a 1. But if I do key => event-archive, value => '1', and compare => =, and type = numberic, it does not return any events. Even though I have an event with the box checked. –  tjsherrill Mar 17 '11 at 20:15
    
I've editied my last post. On my site, this is working as expected. –  rofflox Mar 18 '11 at 9:29

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.