WordPress Tricks #4


So you have custom taxonomy in your WP site, because you want some posts to be a bit more than just “Articles”, follow their own rules and be displayed in a different way maybe.

So what if you want to display, in one page, all those posts BUT first the ones with a specific custom field value?
How do you prevent the Loop to load all the posts, regardless the custom field value difference?

This is how you do it, using 2 custom Loops.

Obviously, replace the snippet with your own custom post type name and custom field name and value, where needed.

	<?php // 1st loop ?>

	<?php // this will display only posts from custom taxonomy post type "PostTypeName", that DO have the custom field value "CustomFieldValue"
	query_posts(array('post_type' => 'PostTypeName', 'meta_value' => 'CustomFieldValue'));
	if(have_posts()) : while(have_posts()) : the_post();
	?>

    <h3><?php the_title(); ?></h3>
    <p><?php the_excerpt(); ?></p>
    <div>some html</div>

	<?php // use this to echo the value of the custom field "CustomFieldName" of the post, where you want, if you need to
	echo get_post_meta($post->ID, 'CustomFieldName', true);
	?>

    <h3><?php the_title(); ?></h3>
    <p><?php the_excerpt(); ?></p>
    <div>some html</div>

	<?php endwhile; endif;
	wp_reset_query(); ?>

So now you’re halfway through with it, let’s see how to grab the rest of the posts, the ones that don’t have that same custom field value, and will be displayed right below all the previous ones.

<?php // 2nd loop ?>

	<?php // this will display only posts from custom taxonomy post type "PostTypeName", that DONT have the custom field value "CustomFieldValue"
	$querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_value <> 'CustomFieldValue'
    AND wposts.post_status = 'publish'
    AND wposts.post_type = 'PostTypeName'
    ORDER BY wposts.post_date DESC
	";

	$pageposts = $wpdb->get_results($querystr, OBJECT);

	if ($pageposts):
		foreach ($pageposts as $post):
		setup_postdata($post); ?>
    
	<h3><?php the_title(); ?></h3>
    <p><?php the_excerpt(); ?></p>
    <div>some html</div>
	
	<?php endforeach; endif; ?>

This second loop, can actually be accomplished also using this snippet:

<?php // 2nd loop alternative ?>

<?php // this will display only posts from custom taxonomy post type "PostTypeName", that DONT have the custom field value "CustomFieldValue"
	query_posts(array('post_type' => 'PostTypeName', 'meta_value' => 'CustomFieldValue', 'meta_compare' => '!=', 'meta_key'  => 'CustomFieldName'));
	if(have_posts()) : while(have_posts()) : the_post(); ?>

	<h3><?php the_title(); ?></h3>
    <p><?php the_excerpt(); ?></p>
    <div>some html</div>

	<?php endwhile; endif;
	wp_reset_query(); ?>

 

Enjoy!

Jany Martelli

I’m a Digital Consultant, Front-End Developer, specialized in WordPress.
I help companies, professionals and startups grow with technology and a winning digital strategy, I hand-pick and coordinate the perfect team for them, plan projects from the ground-up, develop & deploy websites, online stores and apps.