I'm stumped. I've looked at various sites but haven't found the exact code I need.

Basically I want to create a list of custom post titles with the date from one of the custom fields for that post along side.

So I'll end up with a list looking something like this:

Post Title 1 (with link) ...... Associated Custom Field
Post Title 2 (with link) ...... " Custom Field
Post Title 3 (with link) ...... " Custom Field


I want to be able to add this to a page via a short code [boats].

I know I'm on the right track but have really hashed two bits of code together. I'm not sure how to pull the array from the php script (the bit starting $args=array) into extract(short code_atts ...

I'm also not sure about the end part with

<?php }

Here's the mess so far ...

add_shortcode('boats', 'shortcode_boats');
function shortcode_boats($atts){


  'post_type' => 'yacht-for-sale',
  'post_status' => 'publish',
  'posts_per_page' => 15,
  'caller_get_posts'=> 1
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {  ?>
 <h3>List of Available Yachts &amp; Catamarans</h3>
 <div id="boatlist">
  while ($my_query->have_posts()) : $my_query->the_post();
    $price = get_post_meta($post->ID, 'price', true); ?>
    <p class="boatrow"><span class="s-left"><?php the_title(); ?></span><span class="s-right"><?php echo $price; ?></span></p>
  <?php endwhile; ?>
<?php }
add_shortcode("boats", "shortcode_boats");

Just in case this can be used in anyway, I'm currently using the 'Display Posts Shortcode' plugin by Bill Erickson in the page but I need the custom-field 'price' lined up with it.

[display-posts post_type="yacht-for-sale" order="DSC" orderby="title" posts_per_page="20"]

Thanks for any help you can give me on this. I'm not a great php coder ... as you can probably tell. :)



2 Answers 2


Try the following code: (I've not tested it, so there might be few things here and there, but you can get the overall idea):

add_shortcode('boats', 'shortcode_boats');
function shortcode_boats($atts){
    //merge the passed attributes with defaults
                'post_type'         => 'yacht-for-sale',
                'post_status'       => 'publish',
                'posts_per_page'    => 15,
                'caller_get_posts'  => 1 //i am not sure what you are doing with this value

    //now create a seprate agruments array for wp_query using the values from above(extracted variables)
    $args = array(
        'post_type'         => $post_type,
        'post_status'       => $post_status,
        'posts_per_page'    => $posts_per_page


    $my_query = new WP_Query( $args );
    if( $my_query->have_posts() ) {
        <h3>List of Available Yachts &amp; Catamarans</h3>
        <div id="boatlist">
            while ($my_query->have_posts()) : $my_query->the_post();
                $price = get_post_meta( get_the_ID(), 'price', true); ?>
                <p class="boatrow">
                    <span class="s-left"><?php the_title(); ?></span>
                    <?php if( isset( $price ) ) : ?>
                        <span class="s-right"><?php echo $price; ?></span>
                    <?php endif;?>
            <?php endwhile; ?>
    wp_reset_query();//reset the global variable related to post loop
    $retVal = ob_get_contents();

    return $retVal;
  • Hi Webdwall,I got the code from somewhere off the net so not sure what caller_get_posts' => 1 is for, but I've seen some comments elsewhere saying you should use ignore_sticky_posts=1 in place of caller_get_posts. So I'll change that. That you for the corrected code, I'll give it a try and get back. Commented Jun 6, 2013 at 7:52
  • Your code is working, however no matter where I put the shortcode [boats] on a WP page it always displays above the rest of the content, just under the Page title. Any ideas how to stop it doing this? I can't find anything obvious in the code. Thanks, Tracy Commented Jul 9, 2013 at 4:37
  • Sorry for replying so late.. Actually the method which processes any shortocode has to 'return' the html rather than directly 'echoing' it. So You can either use object buffering or take the entire html in a string value and return it.
    – webdwall
    Commented Jul 9, 2013 at 17:46

Have you looked at the List category Posts plugin.


It has done me well for some time, and I feel sure it would be worth looking at for this. Especially the bit that says this:

custom fields - To use custom fields, you must specify two values: customfield_name and customfield_value. Using this only show posts that contain a custom field with this name and value. Both parameters must be defined, or neither will work.

customfield_display - Display custom field(s). You can specify many fields to show, separating them with a coma.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.