Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Loop - Display Post if Custom Field Not Empty WordPress

Anyone got a piece of code laying around that does this?

Custom Post Type: stores
Custom Field: _simple_fields_fieldGroupID_1_fieldID_8_numInSet_0

Only list maximum of 25 posts where the custom field isn't empty. If field is empty, do nothing.

Answers (5)

2010-12-31

Cosmin Popovici answers:

This should do the trick:


<?php

$newquery = new WP_Query();
$newquery->query('post_type=stores&showposts=25');
while($newquery->have_posts()) : $newquery->the_post();

$customField = get_post_custom_values('_simple_fields_fieldGroupID_1_fieldID_8_numInSet_0');

if(!empty($customField)) {

?>

<h1><?php the_title(); ?></h1>
<?php the_content();
//etc., place your content here ?>

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


John Vinch comments:

Will test this in the next few hours. Thanks in advance.


Cosmin Popovici comments:

Just noticed that Rashad's solution is actually far more elegant.

You should go ahead and try that instead, I admit it's much better than mine.

Happy New Year wherever you are :)

2010-12-31

Rashad Aliyev answers:

<?php

if( get_post_meta($post->ID, "_simple_fields_fieldGroupID_1_fieldID_8_numInSet_0", true) ): ?>


if it's not empty here you can use your loop.

<?php else: ?>

If it's empty you can use your code here.

<?php endif; ?>


John Vinch comments:

But how do I restrict the posts to the storetype only and list 25?


Rashad Aliyev comments:

@Cosmin thanks;)

Dear John, you can use it in your loop with custom query.

Use this query : post_type=<strong>store</strong>&posts_per_page=25

For example below shows 25 published custom post (store).


global $wp_query;
$wp_query = new WP_Query("post_type=<strong>store</strong>&post_status=publish&posts_per_page=25");

2011-01-02

Chris Bavota answers:

For code efficiency, you should always create a if statement similar to how Cosmin Popovici has it or else you will get an indexing error when the value of your custom fields is empty.

<?php
$args = array(
"post_type" = "store",
"posts_per_page" = 25
);
query_posts($args);
while ( have_posts() ) : the_post();
$custom_field = get_post_meta($post->ID, "_simple_fields_fieldGroupID_1_fieldID_8_numInSet_0", true);
if(!empty($custom_field)) {
// do your code here
}
endwhile;
?>


Always set a variable with your custom field value first and then check if the variable is empty before performing a loop. The code above stores your custom field value in the variable $custom_field. If the variable has a value, the code will run. If not, nothing will happen and you will not receive any errors.

2011-01-03

Peter Michael answers:

Untested, but should work:

<?php query_posts('post_type=stores&meta_key=_simple_fields_fieldGroupID_1_fieldID_8_numInSet_0&meta_compare=>&meta_value=0&posts_per_page=25'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
...

2011-01-03

Sébastien | French WordpressDesigner answers:

Hi John,

you must use this code :

query_posts('post_type=stores&meta_key=_simple_fields_fieldGroupID_1_fieldID_8_numInSet_0&meta_compare!=&meta_value=&posts_per_page=25');
if ( have_posts() ) : while ( have_posts() ) : the_post();


This code displays 25 posts per page which have a field _simple_fields_fieldGroupID_1_fieldID_8_numInSet_0 not empty

Sebastien

PS : The code of peeter is not bad but display post which have a value > 0 in the field _simple_fields_fieldGroupID_1_fieldID_8_numInSet_0





John, is it ok ? That works ?