NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Web Hosting Canada

Last modified : Sep 03 2022

Estimated reading time : 1 minute, 12 seconds - 105 words

Display Posts with an Post Meta Key empty: With this tutorial, we will create a query that will allow you to display WordPress Posts or Post Types even if they have an empty Post Meta.

With our example, we created a query that will check the viavideos Post Type and link it to Post Meta Key. You can add additional arguments if need be. We will use the showposts to display the quantity.

Here’s the code to add into your template or other and tailor it to your query with meta_query this way:

///// Nous construisons nos arguments requête ///// 
$args = array( 
'post_type' => 'viavideos', 
'showposts' => '500', 
///// Nous construisons nos arguments meta query en spécifiant relation AND ///// 
'meta_query' => array( 
'relation' => 'AND', 
'key' => 'informations', 
'value' => '', 
'compare' => 'NOT EXISTS' 
$via_query = new WP_Query( $args ); 
// The Loop
<?php if ( $via_query->have_posts() ) { while ( $via_query->have_posts() ) { $via_query->the_post(); ?>
<li><article><?php the_title(); ?></article></li>
<?php } ?>
<?php } else { ?>
// no posts found
<?php } ?>

// Restore original Post Data
<?php wp_reset_postdata(); ?>

Don’t forget to update the Post Type and your Post Meta Key.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>