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

WP Query merge with get_post_meta conditions WordPress

  • SOLVED

I am making a sidebar widget that will only appear on my custom post type exhibitors single template.

This widget simply gets Custom Field Meta data from theexhibitors and outputs it in a list format.

Each Custom Field post_meta field has a different purpose and is formatted slightly different. Some are just text and some are hyperlinks.



<strong>MY PROBLEM</strong>

I some how need my WP Query only to run if any of my 6 post_meta's exist.

If none exist, then I don't need any of the list surroundings to appear.

You will see what I tried below but fails miserably.


<?php

$currentID = get_the_ID();
$exhibitorMeta = new WP_Query(array(

'post_type' => 'exhibitors',
'post_id' => $currentID

)); ?>

<?php if ($exhibitorMeta->have_posts()) : ?>

<?php while ($exhibitorMeta->have_posts()) : $exhibitorMeta->the_post(); ?>


<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ||
get_post_meta($post->ID, 'Twitter User', true) ||
get_post_meta($post->ID, 'Web Address', true) ||
get_post_meta($post->ID, 'E-mail Address', true) ||
get_post_meta($post->ID, 'Stand Number', true) ||
get_post_meta($post->ID, 'Hall Number', true)
) { ?>

<div class="module sidebar">

<div class="module-head">

Exhibitor Info

</div>

<div class="module-body">

<ul class="sidebar-list">

<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Facebook Page', true) . '">View our Facebook Page</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Twitter User', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Twitter User', true) . '">Follow us on Twitter</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Web Address', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Web Address', true) . '">View Website</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'E-mail Address', true) )

$emailAddress = get_post_meta($post->ID, 'E-mail Address', true);

echo '

<li><a href="mailto:' . $emailAddress . '" title="' . $emailAddress . '" target="_blank">' . $emailAddress . '</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Stand Number', true) ) echo '

<li>Stand Number: ' . get_post_meta($post->ID, 'Stand Number', true) . '</li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Hall Number', true) ) echo '

<li>Hall Number: ' . get_post_meta($post->ID, 'Hall Number', true) . '</li>

'; ?>

</ul>

</div>

<div class="module-foot"></div>

</div>

<?php } ?>


<?php endwhile; ?>

<?php unset($exhibitorMeta); endif; wp_reset_query(); ?>


You can see I have tried to use PHP || comparison within the WP Query to only show the list surroundings if any of my 6 meta values exist. But for some reason, it just outputs the list surroundings 6 times.

So it's all wrong.

<strong>MY QUESTION</strong>

Is it posible, using the same WP Query information, to retrieve the post_meta data information, but if no post_meta's exist, then the list surroundings don't appear.

But if one or more post_meta's exist, then the list surroundings appear with the active post_meta's appearing in their formated way.

Any would be so great because I can't seem to find much on using multiple post_meta values like this.

Thanks

Answers (24)

2012-07-03

Arnav Joy answers:

try this


<?php



$currentID = get_the_ID();

$exhibitorMeta = new WP_Query(array(



'post_type' => 'exhibitors',

'post_id' => $currentID



)); ?>



<?php if ($exhibitorMeta->have_posts()) : ?>



<?php while ($exhibitorMeta->have_posts()) : $exhibitorMeta->the_post(); ?>


<?php

$fbPage = get_post_meta($post->ID, 'Facebook Page', true);
$twetterUser = get_post_meta($post->ID, 'Twitter User', true);
$webAddress = get_post_meta($post->ID, 'Web Address', true);
$emailAddress = get_post_meta($post->ID, 'E-mail Address', true);
$standNumber = get_post_meta($post->ID, 'Stand Number', true);
$hallNumber = get_post_meta($post->ID, 'Hall Number', true);

?>


<?php if ( !empty( $fbPage ) || !empty( $twetterUser ) || !empty( $webAddress ) || !empty( $emailAddress ) || !empty( $standNumber ) || !empty( $hallNumber ) || ) { ?>



<div class="module sidebar">



<div class="module-head">



Exhibitor Info



</div>



<div class="module-body">



<ul class="sidebar-list">



<?php if ( !empty( $fbPage ) ) echo '



<li><a href="' . $fbPage . '">View our Facebook Page</a></li>



'; ?>



<?php if ( !empty( $twetterUser ) ) echo '



<li><a href="' . $twetterUser . '">Follow us on Twitter</a></li>



'; ?>



<?php if ( !empty( $webAddress ) ) echo '



<li><a href="' . $webAddress . '">View Website</a></li>



'; ?>



<?php if ( !empty( $emailAddress ) )



echo '



<li><a href="mailto:' . $emailAddress . '" title="' . $emailAddress . '" target="_blank">' . $emailAddress . '</a></li>



'; ?>



<?php if ( !empty( $standNumber ) ) echo '



<li>Stand Number: ' . $standNumber. '</li>



'; ?>



<?php if ( !empty( $hallNumber ) ) echo '



<li>Hall Number: ' . $hallNumber . '</li>



'; ?>



</ul>



</div>



<div class="module-foot"></div>



</div>



<?php } ?>





<?php endwhile; ?>



<?php unset($exhibitorMeta); endif; wp_reset_query(); ?>


Josh Cranwell comments:

Think there might be a syntax error. Just kills my sidebar.

I can't see any stray code tho...


Arnav Joy comments:

try this

<?php



$currentID = get_the_ID();

$exhibitorMeta = new WP_Query(array(

'post_type' => 'exhibitors',

'post_id' => $currentID ,

'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'Facebook Page',
'value' => '',
'compare' => '!='
),

array(
'key' => 'Twitter User',
'value' => '',
'compare' => '!='
),


array(
'key' => 'Web Address',
'value' => '',
'compare' => '!='
),

array(
'key' => 'E-mail Address',
'value' => '',
'compare' => '!='
),

array(
'key' => 'Stand Number',
'value' => '',
'compare' => '!='
),

array(
'key' => 'Hall Number',
'value' => '',
'compare' => '!='
),



)





)); ?>



<?php if ($exhibitorMeta->have_posts()) : ?>



<?php while ($exhibitorMeta->have_posts()) : $exhibitorMeta->the_post(); ?>





<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ||

get_post_meta($post->ID, 'Twitter User', true) ||

get_post_meta($post->ID, 'Web Address', true) ||

get_post_meta($post->ID, 'E-mail Address', true) ||

get_post_meta($post->ID, 'Stand Number', true) ||

get_post_meta($post->ID, 'Hall Number', true)

) { ?>



<div class="module sidebar">



<div class="module-head">



Exhibitor Info



</div>



<div class="module-body">



<ul class="sidebar-list">



<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Facebook Page', true) . '">View our Facebook Page</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Twitter User', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Twitter User', true) . '">Follow us on Twitter</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Web Address', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Web Address', true) . '">View Website</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'E-mail Address', true) )



$emailAddress = get_post_meta($post->ID, 'E-mail Address', true);



echo '



<li><a href="mailto:' . $emailAddress . '" title="' . $emailAddress . '" target="_blank">' . $emailAddress . '</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Stand Number', true) ) echo '



<li>Stand Number: ' . get_post_meta($post->ID, 'Stand Number', true) . '</li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Hall Number', true) ) echo '



<li>Hall Number: ' . get_post_meta($post->ID, 'Hall Number', true) . '</li>



'; ?>



</ul>



</div>



<div class="module-foot"></div>



</div>



<?php } ?>





<?php endwhile; ?>



<?php unset($exhibitorMeta); endif; wp_reset_query(); ?>


Josh Cranwell comments:

See I tried this,

But this just outputs my loop contents 4 times.

This is because I have 4 post_meta keys active on the post I'm viewing.

I am trying to only make the wp_query loop once - and the meta keys that are in use, just appear in li's




Arnav Joy comments:

try this

<?php



$currentID = get_the_ID();

$exhibitorMeta = new WP_Query(array(

'post_type' => 'exhibitors',

'post_id' => $currentID ,

'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'Facebook Page',
'value' => '',
'compare' => '!='
),

array(
'key' => 'Twitter User',
'value' => '',
'compare' => '!='
),


array(
'key' => 'Web Address',
'value' => '',
'compare' => '!='
),

array(
'key' => 'E-mail Address',
'value' => '',
'compare' => '!='
),

array(
'key' => 'Stand Number',
'value' => '',
'compare' => '!='
),

array(
'key' => 'Hall Number',
'value' => '',
'compare' => '!='
),



)





)); ?>



<?php if ($exhibitorMeta->have_posts()) : ?>



<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ||

get_post_meta($post->ID, 'Twitter User', true) ||

get_post_meta($post->ID, 'Web Address', true) ||

get_post_meta($post->ID, 'E-mail Address', true) ||

get_post_meta($post->ID, 'Stand Number', true) ||

get_post_meta($post->ID, 'Hall Number', true)

) { ?>



<div class="module sidebar">



<div class="module-head">



Exhibitor Info



</div>



<div class="module-body">



<ul class="sidebar-list">



<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Facebook Page', true) . '">View our Facebook Page</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Twitter User', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Twitter User', true) . '">Follow us on Twitter</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Web Address', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Web Address', true) . '">View Website</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'E-mail Address', true) )



$emailAddress = get_post_meta($post->ID, 'E-mail Address', true);



echo '



<li><a href="mailto:' . $emailAddress . '" title="' . $emailAddress . '" target="_blank">' . $emailAddress . '</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Stand Number', true) ) echo '



<li>Stand Number: ' . get_post_meta($post->ID, 'Stand Number', true) . '</li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Hall Number', true) ) echo '



<li>Hall Number: ' . get_post_meta($post->ID, 'Hall Number', true) . '</li>



'; ?>



</ul>



</div>



<div class="module-foot"></div>



</div>



<?php } ?>




<?php unset($exhibitorMeta); endif; wp_reset_query(); ?>


Josh Cranwell comments:

Thank you so much. This actually works great! I thought I would never get there, but you did it.

Though I don't suppose you could help with something in the code which I did early.


<?php if ( get_post_meta($post->ID, 'E-mail Address', true) )

$emailAddress = get_post_meta($post->ID, 'E-mail Address', true);

echo '

<li><a href="mailto:' . $emailAddress . '" title="' . $emailAddress . '" target="_blank">' . $emailAddress . '</a></li>

'; ?>


I tried to make a variable of the email address post_meta.

This works fine above if 'E-mail Address' post exists. But if it does not exist, it still echos the 'li'

If I do this... it works great - but it's a little long winded...

<?php if ( get_post_meta($post->ID, 'E-mail Address', true) ) echo '
<li><a href="mailto:' . get_post_meta($post->ID, 'E-mail Address', true) . '" title="' . get_post_meta($post->ID, 'E-mail Address', true) . '" target="_blank">' . get_post_meta($post->ID, 'E-mail Address', true) . '</a></li>
'; ?>

2012-07-03

Hardeep Singh answers:

Use get_post_meta function to get the value of the post. If value doesn't exists display the list else do not display.

You wouldn't need to merge the data.


Josh Cranwell comments:

Can you show me what you mean please?

2012-07-03

Lew Ayotte answers:

Try changing your query to only get the post with the post meta... like this:

<?php

$currentID = get_the_ID();

$exhibitorMeta = new WP_Query(array(

'post_type' => 'exhibitors',
'post_id' => $currentID,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'Facebook Page'
),
array(
'key' => 'Twitter User'
),
array(
'key' => 'Web Address'
),
array(
'key' => 'E-mail Address'
),
array(
'key' => 'Stand Number'
),
array(
'key' => 'Hall Number'
)

)

)); ?>


You <strong>may</strong> need to set a value/compare too... so you could try this one if the one above doesn't work:

<?php

$currentID = get_the_ID();

$exhibitorMeta = new WP_Query(array(

'post_type' => 'exhibitors',
'post_id' => $currentID,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'Facebook Page',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'Twitter User',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'Web Address',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'E-mail Address',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'Stand Number',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'Hall Number',
'value' => '%',
'compare' => 'LIKE'
)

)

)); ?>


By the way, you can see other examples here: [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters"]]http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters[[/LINK]]


Josh Cranwell comments:

This method seems so promising. But I can't get anything to display using this...

I take it your additions only make this query run if those meta_keys exist?

My post type is exhibitors and the current id should only get the current post meta info. And this will only display on my custom post type?

<?php

$currentID = get_the_ID();
$exhibitorMeta = new WP_Query(array(

'post_type' => 'exhibitors',
'post_id' => $currentID,
'meta_query' => array(

'relation' => 'OR',

array(
'key' => 'Facebook Page',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'Twitter User',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'Web Address',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'E-mail Address',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'Stand Number',
'value' => '%',
'compare' => 'LIKE'
),
array(
'key' => 'Hall Number',
'value' => '%',
'compare' => 'LIKE'
)

)

));

?>

<?php if ($exhibitorMeta->have_posts()) : ?>

<?php while ($exhibitorMeta->have_posts()) : $exhibitorMeta->the_post(); ?>

<div class="module sidebar">

<div class="module-head">

Exhibitor Info

</div>

<div class="module-body">

<ul class="sidebar-list">

<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Facebook Page', true) . '">View our Facebook Page</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Twitter User', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Twitter User', true) . '">Follow us on Twitter</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Web Address', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Web Address', true) . '">View Website</a></li>

'; ?>

<?php if ( get_post_meta($currentID, 'E-mail Address', true) ) echo '

<li><a href="mailto:' . get_post_meta($currentID, 'E-mail Address', true) . '" title="' . get_post_meta($currentID, 'E-mail Address', true) . '" target="_blank">' . get_post_meta($currentID, 'E-mail Address', true) . '</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Stand Number', true) ) echo '

<li>Stand Number: ' . get_post_meta($post->ID, 'Stand Number', true) . '</li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Hall Number', true) ) echo '

<li>Hall Number: ' . get_post_meta($post->ID, 'Hall Number', true) . '</li>

'; ?>

</ul>

</div>

<div class="module-foot"></div>

</div>

<?php endwhile; ?>

<?php unset($exhibitorMeta); endif; wp_reset_query(); ?>

2012-07-03

John Cotton answers:

Try something like this:


$meta = get_post_custom($post->ID);

$check_keys = array( 'Facebook Page', 'Twitter User', 'Web Address', 'E-mail Address', 'Stand Number', 'Hall Number' );
$all_exist = true;
foreach( $check_keys as $key ) {
if( !array_key_exists( $key, $meta ) || empty($meta[$key][0] )) {
$all_exist = false;
break;
}
}

// Do what you need to do depending on $all_exist...


Josh Cranwell comments:

Can you please show me how I would put my post meta loops inside this?

Is it something like this?

<?php

$meta = get_post_custom($post->ID);

$check_keys = array( 'Facebook Page', 'Twitter User', 'Web Address', 'E-mail Address', 'Stand Number', 'Hall Number' );

$all_exist = true;

foreach( $check_keys as $key ) {

if( !array_key_exists( $key, $meta ) || empty($meta[$key][0] )) {

$all_exist = false;

break;

}

}

{ ?>

<ul class="sidebar-list">



<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Facebook Page', true) . '">View our Facebook Page</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Twitter User', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Twitter User', true) . '">Follow us on Twitter</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Web Address', true) ) echo '



<li><a href="' . get_post_meta($post->ID, 'Web Address', true) . '">View Website</a></li>



'; ?>



<?php if ( get_post_meta($currentID, 'E-mail Address', true) ) echo '



<li><a href="mailto:' . get_post_meta($currentID, 'E-mail Address', true) . '" title="' . get_post_meta($currentID, 'E-mail Address', true) . '" target="_blank">' . get_post_meta($currentID, 'E-mail Address', true) . '</a></li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Stand Number', true) ) echo '



<li>Stand Number: ' . get_post_meta($post->ID, 'Stand Number', true) . '</li>



'; ?>



<?php if ( get_post_meta($post->ID, 'Hall Number', true) ) echo '



<li>Hall Number: ' . get_post_meta($post->ID, 'Hall Number', true) . '</li>



'; ?>



</ul>

<?php } ?>


Josh Cranwell comments:

<?php

$meta = get_post_custom($post->ID);

$check_keys = array( 'Facebook Page', 'Twitter User', 'Web Address', 'E-mail Address', 'Stand Number', 'Hall Number' );

$all_exist = true;

foreach( $check_keys as $key ) {

if( !array_key_exists( $key, $meta ) || empty($meta[$key][0] )) {

$all_exist = false;

break;

}

}

{ ?>

<ul class="sidebar-list">

<?php if ( get_post_meta($post->ID, 'Facebook Page', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Facebook Page', true) . '">View our Facebook Page</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Twitter User', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Twitter User', true) . '">Follow us on Twitter</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Web Address', true) ) echo '

<li><a href="' . get_post_meta($post->ID, 'Web Address', true) . '">View Website</a></li>

'; ?>

<?php if ( get_post_meta($currentID, 'E-mail Address', true) ) echo '

<li><a href="mailto:' . get_post_meta($currentID, 'E-mail Address', true) . '" title="' . get_post_meta($currentID, 'E-mail Address', true) . '" target="_blank">' . get_post_meta($currentID, 'E-mail Address', true) . '</a></li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Stand Number', true) ) echo '

<li>Stand Number: ' . get_post_meta($post->ID, 'Stand Number', true) . '</li>

'; ?>

<?php if ( get_post_meta($post->ID, 'Hall Number', true) ) echo '

<li>Hall Number: ' . get_post_meta($post->ID, 'Hall Number', true) . '</li>

'; ?>

</ul>

<?php } ?>


John Cotton comments:

You can do it more simply than that:



<?php

$meta = get_post_custom($post->ID);

$check_keys = array( 'Facebook Page', 'Twitter User', 'Web Address', 'E-mail Address', 'Stand Number', 'Hall Number' );

$all_exist = true;

foreach( $check_keys as $key ) {
if( !array_key_exists( $key, $meta ) || empty($meta[$key][0] )) {
$all_exist = false;
break;
}
}


if($all_exist) {
?>
<ul class="sidebar-list">
<li><a href="<?php echo $meta['Facebook Page'][0]; ?>">View our Facebook Page</a></li>
<li><a href="<?php echo $meta['Twitter User'][0]; ?>">Follow us on Twitter</a></li>
<li><a href="<?php echo $meta['Web Address'][0]; ?>">View Website</a></li>
<!-- etc etc -->
</ul>

<?php } ?>


Josh Cranwell comments:

Thanks John for you help. But I have solved it from another answer.

I really did try to get this to work, as it is alot less bulky, but stuggled as my PHP knowlegde is not great.

Thank you for you time.

2012-07-03

soulissansall answers:

2012-07-03

ldenalithe answers:

2012-07-03

ilabovitczeno answers:

2012-07-03

yrgoseywalke answers:

2012-07-03

tdeodatoermi answers:

2012-07-03

elidewellmari answers:

2012-07-03

jelkjuni answers:

2012-07-03

sndaulsraymo answers:

2012-07-03

tgblutsony answers:

2012-07-03

edressesbride answers:

2012-07-03

apellhamdeme answers:

2012-07-03

astearmanvonda answers:

2012-07-03

ipkeivan answers:

2012-07-03

ufsung answers:

2012-07-03

oosolen answers:

2012-07-03

ietskicody answers:

2012-07-03

cbagscoach answers:

2012-07-03

atsajay answers:

2012-07-03

rsortorerufus answers:

2012-07-03

rgibrobe answers: