Solved

WP Custom Query using Checkbox Meta Value

Posted on 2014-01-21
4
836 Views
Last Modified: 2014-01-22
I am trying to create two custom queries for a custom post type (CPT) on my Wordpress site. I have added a custom metabox to my CPT which has a single checkbox. In my first query, I am listing all the posts for my CPT that has the checkbox selected. Here is my code:

<?php 

	$args = array(
		'post_type' => 'my-custom-post-type', 
		'posts_per_page' => -1,
		'meta_query' => array( 
			array( 
				'key' => 'meta-checkbox', 
				'value' => 'on' 
			)
		)
	);

	$myposts = get_posts($args);

	if($myposts) {

		echo '<ul>';

		foreach($myposts as $mypost) {
			echo '<li>' . get_the_title($mypost->ID) . '</li>';
		}

		echo '</ul>';
	}

?>

Open in new window

This is working which is great. However, what I'm looking to do with my second query is list all the posts from my CPT that do not have the checkbox selected. I thought this would be straightforward but I just can't seem to get this to work (since not selecting the checkbox does not produce a meta value to reference).

Any help would be greatly appreciated. Thanks!
0
Comment
Question by:Adam
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:eemit
ID: 39798476
- You should always save all your metabox fields regardless on their values.
- Put all the various settings (fields) of a metabox into an array, and have only one record in database for all your metabox values.
0
 
LVL 1

Author Comment

by:Adam
ID: 39798958
Thanks for the reply @eemit. However, I'm not sure if I understand what it is you are suggesting I do.

When a checkbox is selected within a metabox, it is given a meta_value of "on". If the checkbox is not selected, there is no value assigned for that field. So I don't really understand what you mean by your comment:
You should always save all your metabox fields regardless on their values.
Also, as mentioned in my question, I have one metabox with one checkbox field—that's all. So I don't believe there is a need to create an array.

Can you clarify your suggestions if I'm misunderstanding anything?
0
 
LVL 15

Accepted Solution

by:
eemit earned 500 total points
ID: 39800718
Try 'meta_compare' => 'NOT EXISTS' e.g.:

	$args = array(
		'post_type' => 'my-custom-post-type', 
		'posts_per_page' => -1,
		'meta_query' => array( 
			array( 
				'key' => 'meta-checkbox', 
				'value' => 'on',
				'compare' => 'NOT EXISTS' 
			)
		)
	);

Open in new window

0
 
LVL 1

Author Closing Comment

by:Adam
ID: 39801266
Perfect! I knew it had to be something straightforward. Thanks @eemit.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question