Solved

WP Custom Query using Checkbox Meta Value

Posted on 2014-01-21
4
830 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Read about why website design really matters in today's demanding market.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now