Solved

WP Custom Query using Checkbox Meta Value

Posted on 2014-01-21
4
850 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

756 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