- 27 July 2022
- WordPress (25)
List all posts that have a specific value (example.com, in this example) in a custom field (Fixed Banner URL, in this case):
<?php
$posts = query_posts( array(
'meta_query' => array(
array(
'key' => 'Fixed Banner URL',
'value' => 'example.com'
)
)
) );
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
List all posts that have a value in a custom field (Fixed Banner URL, in this case), whatever the value (list them all):
<?php
$posts = query_posts( array(
'meta_query' => array(
array(
'key' => 'Fixed Banner URL',
'value' => array(''),
'compare' => 'NOT IN'
)
)
) );
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
List all posts that have a value in a custom field (Fixed Banner URL, in this case), which includes ‘test’ in the value (not exact, just somewhere in the URL):
<?php
$posts = query_posts( array(
'meta_query' => array(
array(
'key' => 'Fixed Banner URL',
'value' => 'test',
'compare' => 'LIKE'
)
)
) );
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Comments
Whether you have feedback, a question, want to share your opinion, or simply want to say thank you - we welcome comments! Please read the disclaimer.