Use strpos (PHP manual). Example:
<?php
$variable = get_post_meta( get_the_ID(), 'meta-key', true );
if( strpos( $variable, '<a' ) !== false ) : ?>
<!-- Do stuff -->
<?php endif; ?>
Where meta-key is the key of the custom field and <a is an example of a fragment that is indicative of a link. I am assuming that you will be using this within the Loop, hence my suggested use of get_the_ID().
Cyril, thank you so much for your response. I should have just put in my code first, and will here below. I attempted your strpos function method, and then another function strpbrk from the link that you gave me. The problem with both is that they are both looking for a string when in fact I am feeding them an array. I get this error:
Warning: strpos() expects parameter 1 to be string, array given
Here is my code. Again to explain, the top portion is simply getting all my pages to list using ‘normal’ WordPress functions without using a db query. The bottom portion, from the $keys_to_show variable on down, is the bit that I’m trying to figure out. Being totally candid, I am worthless at PHP and pieced this together from trial and error, but it works. I left the strpos line in there that is giving me the error–maybe I’m putting it in the wrong place? Maybe there isn’t a function that looks through arrays?
<?php
$mypages = get_pages( array( 'child_of' => 8, 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) );
foreach( $mypages as $page ) { ?>
<div class="document-page-result">
<h5><a href="<?php echo get_page_link($page); ?>"><?php echo $page->post_title; ?></a></h5>
<?php $meta_values = get_post_meta($page->ID);
$keys_to_show = array('custom_field_key_1', 'custom_field_key_2', 'custom_field_key_3','custom_field_key_4', 'custom_field_key_5'); // array of keys to show
if (strpos( $keys_to_show, '<a' ) !== false ); {
foreach ($keys_to_show as $key) {
if( array_key_exists($key, $meta_values) ) {
$values = $meta_values[$key];
foreach ($values as $value) {
echo "$value<br />";
}
}
}
}
?>
</div>
<?php } ?>
Getting back to the original problem/question–attempting to look through all the custom field keys and only display the ones that are links, instead of all of them like it is doing now. Thanks again in advance, I genuinely appreciate it.
Your code is rather convoluted, which is why you’re having trouble working out how to integrate strpos. Try something like this (I haven’t tested it myself but it should be roughly what you’re looking for):
<?php
$mypages = get_pages( array( 'child_of' => 8, 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) );
foreach( $mypages as $page ) : ?>
<div class="document-page-result">
<h5><a href="<?php echo get_page_link($page); ?>"><?php echo $page->post_title; ?></a></h5>
<?php
$keys_to_show = array( 'custom_field_key_1', 'custom_field_key_2', 'custom_field_key_3', 'custom_field_key_4', 'custom_field_key_5' );
foreach( $keys_to_show as $key ) :
$meta_value = get_post_meta( $page->ID, $key, true );
if( strpos( $meta_value, '<a' ) !== false ) :
echo $meta_value . '<br />';
endif;
endforeach; ?>
</div>
<?php endforeach; ?>
Also, a quick piece of advice: avoid using curly braces when you’re layering control structures. It inevitably leads to a labyrinth of closing braces and confusion about where a particular control structure ends. Alternative syntax (as demonstrated in both of my replies) makes it much easier to see what you’re doing.
Cyril, I simply cannot thank you enough. Not only is your code much cleaner and easier to read/edit–it works perfectly! I will avoid curly braces in my future endeavors to learn proper PHP. Again, thank you for your selfless effort to help me, it is truly appreciated! DF