I am trying to pull in a custom field vales for each page while using wp_list_pages. For example I want to list out each portfolio item in my sidebar but also show why type of work it is by pulling in a custom field value so it would look like this:

  • Portfolio Item 1 Type: Web Design
  • Portfolio Item 2 Type: Web Development

Here is the code I am using to pull in custom post types and this works perfectly, I just have no idea how to have it pull custom field values as well:

function wp_list_post_types( $args ) {
$defaults = array(
    'numberposts'  => -1,
    'offset'       => 0,
    'orderby'      => 'menu_order, post_title',
    'post_type'    => 'our-work',
    'depth'        => 0,
    'show_date'    => '',
    'date_format'  => get_option('date_format'),
    'child_of'     => 0,
    'exclude'      => '',
    'include'      => '',
    'title_li'     => __(''),
    'echo'         => 1,
    'link_before'  => '',
    'link_after'   => '',
    'exclude_tree' => '' );

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

$output = '';
$current_page = 0;

// sanitize, mostly to keep spaces out
$r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);

// Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
$exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
$r['exclude'] = implode( ',', apply_filters('wp_list_post_types_excludes', $exclude_array) );

// Query pages.
$r['hierarchical'] = 0;
$pages = get_posts($r);

if ( !empty($pages) ) {
    if ( $r['title_li'] )
        $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';

    global $wp_query;
    if ( ($r['post_type'] == get_query_var('post_type')) || is_attachment() )
        $current_page = $wp_query->get_queried_object_id();
    $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);

    if ( $r['title_li'] )
        $output .= '</ul></li>';

$output = apply_filters('wp_list_pages', $output, $r);

if ( $r['echo'] )
    echo $output;
    return $output;



If all you want is to list some recent portfolio items in the sidebar, you're overkilling it by trying to emulate the wp_list_pages function. You can do this with a simple custom query. (Unless you really want to do this with wp_list_pages for some reason, but I can't think of why you would.)

// Set up the query args
$arr_query_args = array(
    'numberposts'  => 5, // Or however many posts you want to show
    'orderby'      => 'post_title',
    'post_type'    => 'our-work'

// Run the query
$arr_posts = get_posts( $arr_query_args );

// Global the post obj
global $post;

foreach( $arr_posts as $this_post ) { // For every post retrieved

    // Now you have the (mostly) complete post object. Print out whatever you want from it!

    $permalink = get_permalink($this_post->ID); // Get the post's permalink

    echo '<a href="' . $permalink . '"';

    // If this link is for the current page, add a class
    if(is_object($post) && $post->ID == $this_post->ID) {
        echo ' class="current_page_item"';

    echo '>';
        echo $this_post->post_title . ': ';
    echo '</a>';

    // You can use get_post_meta to grab meta values for this post
    echo(get_post_meta($this_post->ID, 'your-custom-field-key', true));


I suppose you could wrap that in a function and use it as a template tag if you want to pass different args to it in different places.

  • Yeah, there aren't any filters in Walker_Page to allow you to easily modify the output text. I'd use a method like this. – goldenapples Jan 5 '11 at 17:38
  • Thanks for the reply. But no I am not just trying to list some recent portfolio items. I am trying create a dynamic menu of portfolio projects in my sidebar, and I need to utilize the dynamic classes of the wp_list_pages function. (ie. "current_page_item) How would I moidify the above code to not only list the page name/link but also a meta value as well? Thanks! – Red Jan 6 '11 at 20:26
  • @Joshua - "How would I modify the above code to not only list the page name/link but also a meta value as well?" Look at the last line in the supplied code--it outputs any meta value you want. Just change "your-custom-field-key" to the name of your custom field key. – MathSmath Jan 6 '11 at 21:10
  • To add a class if it's the current page, do global $post, and then check during the "for every post" loop to see if the global $post->ID matches the current post id. If it does, print out a "current_page_item" class. – MathSmath Jan 6 '11 at 21:15
  • This is great MathSmath, thank you! Could you help me one last time with the last step for adding a current_page_item class... I am unsure how to exactly write this into the loop. Thanks again for you help! – Red Jan 7 '11 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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