UPDATE: I think I'm on the right path with some of the help from the meta query options, but I might've gone overboard as it's extremely inefficient. You can see the updated code in the bottom of this post.
I technically have three questions for this, but they're related and require the first to be answered, so I hope it's okay to post all here.
To put this in a little context, I'm working on a directory of sorts where I can add to 3 URLs in 3 meta fields for each post. I want to display a list of only posts that have 1-3 of said URLs added. To make this extra interesting, I also have 3 corresponding checkboxes to each meta field that I can select in case an item is sold out. I would like to exclude sold out items from this list as well.
Question 1 - How do I display a list of custom post type posts that all have the same meta_value and key (excluding those that have the same value)
I've managed to make a list that returns all myPostType
posts where 'meta_value' => false
in myMetaKey
. See code below:
<?php
$args = array(
'post_type' => 'myPostType',
'meta_key' => 'myMetaKey',
'meta_value' => false,
'orderby' => '_custom',
'order' => 'ASC',
'posts_per_page' => -1, );
$loop = new WP_Query($args);
while ($loop->have_posts() ) : $loop->the_post();
?>
I am interested in having a list that returns posts that does have a meta_value
in myMetaKey
. I thought it would be as simple as to change false
to true
in meta_value
, but that returns an empty list instead.
SOLVED: Question 2 - How do I return a list that includes posts with different meta_key
's?
I'm interested in also having the list display posts from myPostType
that have both myMetaKeyA
, myMetaKeyB
and myMetaKeyC
- but only if meta_value
is true (or the equivalent as asked above in questions 1).
Thanks to Niels who directed me to the
meta_query
class. Adding multiple arrays tometa_query
did the trick.
Question 3 - How do I exclude posts that have a meta_key
with a corresponding checkbox selected
As mentioned in the beginning I'm also interested in excluding posts via a selected checkbox.
I think I fixed a lot of my issues, but my code is so inefficient that it's practically useess.
As it is below it takes more than 30 mins. to load (as in I closed the tab after 30 mins.). Does anyone have any pointers as to how to rewrite this more efficiently?
<?php
$args = array(
'post_type' => array('myPostType1', 'myPostType2'),
'orderby' => '_custom',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array( 'relation' => 'OR',
array('relation' => 'AND',
array(
'key' => 'product1',
'value' => false,
'compare' => '!=',
),
array(
'key' => 'stock1',
'value' => '"soldOut1"',
'compare' => 'NOT LIKE',
)
),
array('relation' => 'AND',
array(
'key' => 'product2',
'value' => false,
'compare' => '!=',
),
array(
'key' => 'stock2',
'value' => '"soldOut2"',
'compare' => 'NOT LIKE',
)
),
array('relation' => 'AND',
array(
'key' => 'product3',
'value' => false,
'compare' => '!=',
),
array(
'key' => 'stock3',
'value' => '"soldOut3"',
'compare' => 'NOT LIKE',
)
)
)
);
$loop = new WP_Query($args);
while ($loop->have_posts() ) : $loop->the_post();
?>