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.

This is the shortcode I have come up with, according to a tutorial on Dig WP (http://digwp.com/2010/01/custom-query-shortcode/) in order to display list of songs on specific albums.

[loop the_query="post_type=song&albumtype=enc&order=ASC&showposts=-1"]

Above is an example, using a custom taxonomy in order to achieve what I want. This returns a list item with the title and permalink of/to the song post type item.

However, I would also like to return data from a couple of custom fields associated to the song post type, such as "Duration". I tried to insert this the way I would in a template, but it did not return anything.

Where should I add what to the below code in order to display the value of the custom field next to the link?

function custom_query_shortcode($atts) {

// EXAMPLE USAGE:
// [loop the_query="showposts=100&post_type=page&post_parent=453"]

// Defaults
extract(shortcode_atts(array(
  "the_query" => ''
), $atts));

// de-funkify query
$the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
$the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);

// query is made               
query_posts($the_query);

// Reset and setup variables
$output = '';
$temp_title = '';
$temp_link = '';

// the loop
if (have_posts()) : while (have_posts()) : the_post();

  $temp_title = get_the_title($post->ID);
  $temp_link = get_permalink($post->ID);

  // output all findings - CUSTOMIZE TO YOUR LIKING
  $output .= "<li><a href='$temp_link'>$temp_title</a> I WANT MY CUSTOM FIELD VALUE HERE</li>";

endwhile; else:

  $output .= "nothing found.";

endif;

wp_reset_query();
return $output;

}
add_shortcode("loop", "custom_query_shortcode");

The above code is from my functions.php file, which is obvious.

share|improve this question

2 Answers 2

You simply need to query your custom fields and return them with your shortcode:

// EXAMPLE USAGE:
// [loop the_query="showposts=100&post_type=page&post_parent=453"]
function custom_query_shortcode($atts) {
    // Defaults
    extract(shortcode_atts(array(
      "the_query" => ''
    ), $atts));

    // de-funkify query
    $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
    $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);

    // query is made               
    query_posts($the_query);

    // Reset and setup variables
    $output = '';
    $temp_title = '';
    $temp_link = '';

    // the loop
    if (have_posts()){
        while (have_posts()){
            the_post();
            $temp_title = get_the_title($post->ID);
            $temp_link = get_permalink($post->ID);
            //get you custom fields
            $duration = get_post_meta( $post->ID, 'duration', true );
            $other_custom_field = get_post_meta( $post->ID, 'other_custom_field_key', true );
            //add the custom field to the output var
            $output .= "<li><a href='$temp_link'>$temp_title</a> Duration: $duration, Other: $other_custom_field</li>";
        }
    }else{
        $output .= "nothing found.";
    }
    wp_reset_query();
    return $output;
}
add_shortcode("loop", "custom_query_shortcode");
share|improve this answer
    
This is what I did before I asked, but no value is returned, should I add anything to the shortcode itself? –  Iva Mar 26 at 9:34
    
NO, simply replace duration with the actual custom field key in the get_post_meta( $post->ID, 'duration', true ); call. –  Bainternet Mar 26 at 9:48
    
The name of the key IS 'duration', but nothing shows next to the link. :( –  Iva Mar 26 at 10:00
    
then something else is wrong in your setup... try adding var_dump(get_post_custom($post->ID)); inside the loop and see how and what data is stored. –  Bainternet Mar 26 at 10:02
1  
Its 'Duration' in your end so change 'duration' to 'Duration'. –  Bainternet Mar 26 at 13:15
up vote 0 down vote accepted

A friend pointed out how to solve this. If anybody else runs into the same issue as me, this is the correct code.

// Custom shortcode, to query tracklistings.
function custom_query_shortcode($atts) {

// Defaults
extract(shortcode_atts(array(
  "the_query" => ''
), $atts));

// de-funkify query
$the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
$the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);

// query is made               
$query_posts = get_posts($the_query);

// Reset and setup variables
$output = '';

// the loop
if (!empty($query_posts)) {
    foreach($query_posts as &$post_object) {
        $output .= '<li><a href="'. get_permalink($post_object->ID) .'">'. $post_object->post_title .'</a> <span>'. get_post_meta($post_object->ID, 'Duration', true) .'</span></li>';
    }
}
return $output;
}
add_shortcode("loop", "custom_query_shortcode");
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.