Solved

WP Custom Query using Checkbox Meta Value

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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.
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 …
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
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 …

706 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

19 Experts available now in Live!

Get 1:1 Help Now