NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Sep 03 2022
Estimated reading time : 1 minute, 12 seconds - 105 words
Share the post "Display a post list if Post Meta Key is empty"
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:
<?php ///// 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', array( '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.