Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

WP Custom Query using Checkbox Meta Value

Posted on 2014-01-21
4
Medium Priority
?
971 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to dynamically set the form action using jQuery.
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 …
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

916 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