I have an existing website. And I set up lots of custom fields.
But I was unaware custom fields are essentially the meta key - which correct me if I'm wrong, a meta should not have any spaces if you want to use in a meta_query? Maybe this is the reason why my meta query is not working (I hope not!)
Anyway, if I'm wrong I need help please with this query below...
<?php
$hallNumber = '2';
$hall2 = new WP_Query(array(
'post_type' => 'exhibitor',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'Hall Number',
'value' => $hallNumber,
'compare' => '=='
)
));
if ($hall2->have_posts()) : ?>
<ul>
<?php while ($hall2->have_posts()) : $hall2->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php unset($hall2); endif; wp_reset_query(); ?>
This is what I have so far, but it is not working as it should. Cleanest query fix will get the prize fund.
What my query needs to do...
<strong>Show posts from my custom post type 'exhibitor', with my meta key 'Hall Number' value of X, and order by post title alphabetically.
</strong>
I thought my query above would work OK, but it just returns all my posts from 'exhibitor'
Thanks
John Cotton answers:
Your compare should be '='
Josh Cranwell comments:
Thanks,
But still returns all 'exhibitor' posts?
John Cotton comments:
'meta_query' => array(array(
'key' => 'Hall Number',
'value' => $hallNumber,
'compare' => '='
) )
Josh Cranwell comments:
$hall2 = new WP_Query(array(
'post_type' => 'exhibitor',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'Hall Number',
'value' => '2',
'compare' => '='
)
));
Just returns entire post - see this screenshot from my database... [[LINK href="http://i.imgur.com/Wq8UT.png"]]http://i.imgur.com/Wq8UT.png[[/LINK]]
John Cotton comments:
<blockquote>Just returns entire post</blockquote>
You didn't copy my code - look more closely - there is an array within an array...
Josh Cranwell comments:
I did not... I did not notice the extra array. It works.
Thank you so much!!!!
Michael Caputo answers:
Your key should not have any spaces. Your value can have spaces.
Michael Caputo comments:
Also as per the WP Doc:
<blockquote>compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'NOT EXISTS' (in WP 3.5). Default value is '='.</blockquote>
There is no '==' operator.
Josh Cranwell comments:
Would that explain why my query does not work?
Damn, all my custom fields have spaces.
Josh Cranwell comments:
See my table... [[LINK href="http://i.imgur.com/Wq8UT.png"]]http://i.imgur.com/Wq8UT.png[[/LINK]]