1

I'm trying to get all posts that don't have a meta ket that starts with aa_. I can do it this way

$args = array(
'posts_per_page' => -1,
'category__in' => 23,
'meta_query' => array(
'relation' => 'AND',
array(
 'key' => 'aa_article_1',
 'compare' => 'NOT EXISTS', 
 'value' => '' 
),
array(
 'key' => 'aa_article_2',
 'compare' => 'NOT EXISTS', 
 'value' => '' 
),
array(
 'key' => 'aa_article_3',
 'compare' => 'NOT EXISTS', 
 'value' => '' 
),
array(
 'key' => 'aa_article_4',
 'compare' => 'NOT EXISTS', 
 'value' => '' 
),
array(
 'key' => 'aa_article_5',
 'compare' => 'NOT EXISTS', 
 'value' => '' 
),
array(
 'key' => 'aa_article_6',
 'compare' => 'NOT EXISTS', 
 'value' => '' 
)

));

$the_query = new WP_Query ($args);
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li><a href="' . get_the_permalink() . '" target="_blank">' . get_the_title() . '</a></li>';
}
echo '</ul>';
echo "<h2>Found: $the_query->found_posts posts</h2>";
} else {
// no posts found
}
wp_reset_postdata();

Is there a way to do this simpeler? I have about 50 meta keys that start with aa_ and it would save some time. And of course it's also nice to know if it's possible, for future use :)

  • This is a tricky one, I'm not sure if a huge meta_query with 50 sub arrays are possible, and even if it is, it would be rather slow. We could try to change the = operator in the SQL query to LIKE for meta key matching, then we could match meta keys starting with aa_. Again, LIKE comparisons are really slow, and it becomes an issue on very large databases. IMHO, I would create a new custom field, and assing 1 to it when a post has a field starting with aa_, and 0 if not. We can then query posts by this new custom field – Pieter Goosen Apr 8 '16 at 6:32
  • I would also just try to keep my meta key as aa_article and then asiing the number to the meta value – Pieter Goosen Apr 8 '16 at 7:02
  • maybe a single aa_article meta key with an array that is only set if one of the 50 is set would be better practice here, then you could just check if that exists. – majick Apr 8 '16 at 12:20
  • @Pieter Goosen The new aa_ would be a great idea, but there are already more than 500 posts... – WendiT Apr 9 '16 at 3:59
  • @Pieter Goosen The aa_article won't work in this case but the actual meta keys are like aa_chickensoup, aa_tomatosoup. – WendiT Apr 9 '16 at 4:01
-1

global $wpdb;

$excludeposts = "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND meta_key NOT LIKE %aa_% ORDER BY post_date DESC ";

$main_query = new WP_Query( array( 'post__not_in' => $excludeposts, 'paged' => $paged ) );

while ($main_query->have_posts()) :

$main_query->the_post();
//Stuff...

endwhile;

//hope this helps

  • Please clean up your code and explain how it works and how it should be used – Pieter Goosen Apr 8 '16 at 7:04
  • I am going to explore this and see if I could get it working. – WendiT Apr 9 '16 at 4:02
  • No output whatsoever. I've put <a href="<?php the_permalink() ?>"><?php the_title(); ?></a> where you wrote //stuff – WendiT Apr 9 '16 at 4:26
  • The above code just Select everything except the meta to exclude : - You can run the sql on your db to check the result. just echo the $excludeposts; for a development test and copy paste that sql to your db - Use as get_the_permalink() & get_the_title() – Aishan Apr 10 '16 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.