• I’m looking for some help putting together some code that checks all of the posts in the database for a custom field. If the custom field exists, then it gets the data assigned to that custom field.

    Here is the code I’ve been working with, but I can’t get it to work. Help!

    <?php
    /*
    Template Name: Rest List
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    
        <div id="contentleft">
    <ul>
     <?php
      $postslist = get_posts('numberposts=-1&order=ASC&orderby=title');
     foreach ($postslist as $post) :
    $customPostID = get_post_custom($post->ID);
    if(isset($customPostID['restlist'])) :
        setup_postdata($post);
    echo '<li><a href="' . get_the_permalink() . '">' . $customPostID['restlist'] . '</a></li>';
    endif;
    endforeach;
    ?>
    </ul>    
    
        </div>
    
    <?php include(TEMPLATEPATH."/sidebar_right_page.php");?>
    
    </div>
    
    <!-- The main column ends  -->
    
    <?php get_footer(); ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • get_posts('numberposts=-1&order=ASC&orderby=title&meta_key=SOMEKEY&meta_value=SOMEVALUE');

    Do the meta bit in the get_posts function, it supports getting posts that match particular meta data… 😉

    http://codex.wordpress.org/Template_Tags/get_posts

    Thread Starter scotthack-1

    (@scotthack-1)

    t310s_,

    Thanks! That moved me a step closer. I also fixed the permlink function error. Here is my current code. However, I’m trying to pull the meta_value and make that the anchor text. Right now it just says “Array” which obviously is not right. Thoughts?

    <?php
    /*
    Template Name: Rest List
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    
    	<div id="contentleft">
    <ul>
     <?php
      $postslist = get_posts('numberposts=-1&order=ASC&orderby=title&meta_key=restlist');
     foreach ($postslist as $post) :
    $customPostID = get_post_custom($post->ID);
    if(isset($customPostID['restlist'])) :
        setup_postdata($post);
    echo '<li><a href="' . get_permalink() . '">' . $customPostID['restlist'] . '</a></li>';
    endif;
    endforeach;
    ?>
    </ul>	
    
    	</div>
    
    <?php include(TEMPLATEPATH."/sidebar_right_page.php");?>
    
    </div>
    
    <!-- The main column ends  -->
    
    <?php get_footer(); ?>
    <ul>
    <?php
    $postslist = get_posts('numberposts=-1&order=ASC&orderby=title&meta_key=restlist');
    foreach ($postslist as $post) :
    	$custom = get_post( $post->ID , 'restlist' , true ); // Single value, first match - string
    	//$custom = get_post( $post->ID , 'restlist' , false ); // All matches - array
    	echo '<li><a href="' . get_permalink() . '">' . $custom . '</a></li>';
    	//echo '<li><a href="' . get_permalink() . '">' . implode( ', ' , $custom ) . '</a></li>';
    endforeach;
    ?>
    </ul>

    The commented bits are an example for returning multiple results..

    Just change the bit i’ve posted.. off to bed now, so hope that helps..

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Help with template to pull custom field data’ is closed to new replies.