Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Created February 16, 2023 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/6cb8fe9958c0e7f49643e628450f16db to your computer and use it in GitHub Desktop.
Save nfsarmento/6cb8fe9958c0e7f49643e628450f16db to your computer and use it in GitHub Desktop.
Show posts with and without meta_key and order them the ones with meta_key on the top
<?php
$args = array(
'post_type' => 'projects',
'posts_per_page' => 9,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'featured-checkbox',
'value' => 'yes',
'compare' => 'EXISTS'
),
array(
'key' => 'featured-checkbox',
'compare' => 'NOT EXISTS'
)
),
'orderby' => 'meta_value_num title',
'order' => 'ASC',
);
$featured = new WP_Query($args);
if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
<div class="c-image"><?php the_post_thumbnail('large'); ?></div>
<div class="cstext">
<article>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<a href="<?php echo get_permalink(); ?>" class="readmore">Learn More</a>
</article>
</div>
<?php
endwhile;
else: 'No Content Added';
endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment