Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

etc.

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

</div>
<?php }
wp_reset_query();
}

Here's the mess so far ...

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

extract(shortcode_atts

$args=array(
  '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">
  <?php
  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; ?>
  </div>
<?php }
wp_reset_query();
}
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. :)

Cheers,

Tracy

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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
    extract(
        shortcode_atts(
            array(
                '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
            ),
            $atts
        )
    );

    //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
    );

    ob_start();

    $my_query = new WP_Query( $args );
    if( $my_query->have_posts() ) {
        ?>
        <h3>List of Available Yachts &amp; Catamarans</h3>
        <div id="boatlist">
            <?php
            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;?>
                </p>
            <?php endwhile; ?>
        </div>
        <?php 
    }
    wp_reset_query();//reset the global variable related to post loop
    $retVal = ob_get_contents();
    ob_end_clean();

    return $retVal;
}
share|improve this answer
    
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. –  Tracy Shorrock Jun 6 '13 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 –  Tracy Shorrock Jul 9 '13 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 Jul 9 '13 at 17:46

Have you looked at the List category Posts plugin.

http://wordpress.org/plugins/list-category-posts/other_notes/

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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