Support » Fixing WordPress » Display posts with the same custom field value as the current post

  • Resolved tgildred


    Hi, I’ve searched everywhere and can’t find an exact answer for what I’m trying to do.

    I have given my posts a set of custom fields, for instance: project_type = magazine. These fields are then displayed in the post on my site. I’d like to be able to turn that field value in each post into a link that, when clicked, would take the user to listing of all posts of that project type. The same way that clicking a tag or category would take you to all posts with that tag or category. I just can’t figure out how to accomplish this, though.

    Here’s the code from single.php:

    <?php get_header(); ?>
    	<div id="content">
    		<?php if (have_posts()) : ?>
    			<?php the_post(); ?>
    			<div class="post">
    				<?php skimmed_milk_post_title('h2'); ?>
    				<div class="entry">
    					<?php skimmed_milk_post_entry(); ?>
    				</div><!-- entry -->
                    <div class="metadata">
    	                <div class="top"></div>
                    	<div class="navigation">
    						<div class="alignleft"><?php next_post_link('&laquo; %link', 'Previous', TRUE); ?></div>
    						<div class="alignright"><?php previous_post_link('%link &raquo;', 'Next', TRUE); ?></div>
    						<div class="snap-to-fit"></div>
    					</div><!-- navigation -->
                    	<p class="postmetadata alt ">
    							$dateString = __('F jS, Y', 'skimmed');
    							printf(__('Posted in %1$s on %2$s', 'skimmed'),
    								get_the_category_list(', '),
    								apply_filters('the_time', get_the_time($dateString), $dateString),
    								apply_filters('the_time', get_the_time(''), ''));
    							the_tags('<br />' . __('Tagged with ', 'skimmed'), ', ', ' ');
    						<br />Project Type: <?php if ( function_exists('get_custom_field_value') ){ get_custom_field_value('project_type', true); } ?>
    						<br />Client: <?php if ( function_exists('get_custom_field_value') ){ get_custom_field_value('client', true); } ?>
    						<br />Description: <?php if ( function_exists('get_custom_field_value') ){ get_custom_field_value('description', true); } ?>
                    <div class="bottom"></div>
    			</div><!-- post -->
    			<?php comments_template();
    		else : // no posts
    			skimmed_milk_something_not_found(__('Post not found', 'skimmed'));
    		endif; // end if have posts ?>
    	</div><!-- content -->
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    And the archive.php:

    <?php get_header(); ?>
    	<div id="content">
    		<?php if (have_posts()) :
    			if (is_category()) $archive_display_name = single_cat_title('', false);
    			elseif (is_tag()) $archive_display_name = single_tag_title('', false);
    			elseif (is_day()) $archive_display_name = get_the_time(__('F jS, Y', 'skimmed'));
    			elseif (is_month()) $archive_display_name = get_the_time(__('F Y', 'skimmed'));
    			elseif (is_year()) $archive_display_name = get_the_time(__('Y', 'skimmed'));
    			<?php if (is_category()) : ?>
    				<h2 class="pagetitle"><span class="darkorange">/</span><span class="lightorange">/</span> <?php printf(__('%s', 'skimmed'),  $archive_display_name); ?></h2>
    			<?php elseif (is_tag()) : ?>
    				<h2 class="pagetitle"><?php printf(__('Posts tagged with %s', 'skimmed'),  $archive_display_name); ?></h2>
    			<?php else : ?>
    				<h2 class="pagetitle"><?php printf(__('%s Archive', 'skimmed'), $archive_display_name); ?></h2>
    			<?php endif;
    			skimmed_milk_nav_link(sprintf(__('Later %s posts', 'skimmed'), $archive_display_name), '');
    			while (have_posts()) : the_post();
    				global $page; $page = 1; ?>
    				<div class="thumb">
    					<div class="thumb_excerpt">
    						<?php the_excerpt('<span class="nowrap">' . __('Read more', 'skimmed') . ' &raquo;</span>'); ?>
    					</div><!-- entry -->
                        <div class="thumb_title">
                            <?php if ( get_post_meta($post->ID, 'project_type', true) ) { ?>
    						<h3><a href="<?php echo get_permalink(); ?>" rel="bookmark" title="<?php printf(__("Permanent link to '%s'", 'skimmed'), the_title_attribute(array('echo' => false))); ?>"><?php echo get_post_meta($post->ID, "project_type", $single = true); ?><?php } ?></a></h3>
    						<?php if( ! is_year()) : // don't show content if is full year archive ?>
    					<?php endif; ?>
    					<!-- <?php trackback_rdf(); ?> -->
    				</div> <!-- post -->
    			<?php endwhile;
    			skimmed_milk_nav_link('', sprintf(__('Earlier %s posts', 'skimmed'), $archive_display_name));
    		else : // no posts
    			skimmed_milk_something_not_found(__('Archive not found', 'skimmed'));
    		endif; // end if have posts ?>
    	</div><!-- content -->
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    I’ve reached the limits of abilities with this one. If anyone could point me in the direction of how to accomplish this, I would really appreciate it. I’m very new at this, so if there’s any information I’ve left out, please let me know!


Viewing 9 replies - 1 through 9 (of 9 total)
  • Might want to see if this plugins will help:

    You may want to look at using custom taxonomies for this.

    Custom taxonomies in WordPress 2.8

    It would work the way you envisioned with tags and categories more or less out of the box. The above post has plenty of examples in it.

    Hi… thanks for the feedback. Both of these seem very promising, but I think custom taxonomies is exactly what I’m looking for. I’ve read the posts about custom taxonomy at the web site provided above and have read numerous other articles about this, and have successfully enabled my custom taxonomy by adding the following to my functions.php file:

    add_action( 'init', 'create_my_taxonomies', 0 );
    function create_my_taxonomies() {
    	register_taxonomy( 'media_type', 'post', array( 'hierarchical' => false, 'label' => 'Media Type', 'query_var' => true, 'rewrite' => true ) );
    	register_taxonomy( 'client', 'post', array( 'hierarchical' => false, 'label' => 'Client', 'query_var' => true, 'rewrite' => true ) );
    	register_taxonomy( 'project', 'post', array( 'hierarchical' => false, 'label' => 'Project	', 'query_var' => true, 'rewrite' => true ) );

    I then added the following code to single.php:

    <br /><?php echo get_the_term_list( $post->ID, 'media_type', 'Media Type: ', ', ', '' ); ?>

    Otherwise, single.php is the same as the code in my original post. What I can’t figure out is how to get that to output all posts with a media_type: magazine to my archive.php. I’ve tried numerous changes to my archive.php code, but for simplicity’s sake, please see my original post for my archive.php code, since my attempts were just shots in the dark. Any thoughts? I’m sure I’m missing something simple. I hope, anyway. Thanks again for the help, and in advance for any forthcoming help. 🙂

    Small update to my last post: I changed

    ‘rewrite’ => true


    ‘rewrite’ => false

    And it magically worked. I have no idea why. As I’d like to have the prettier links, I guess my original question still stands. I should note that I’m developing this thing on a local host, so the “pretty” URL rewrite looks like this “http://localhost:8888media_type/magazine” and the “ugly” non-rewritten URL is “http://localhost:8888/?media_type=magazine”

    I wonder if once this is live, the problem will go away? I’ll also add this question: is there a custom taxonomy equivalent of “is_category” and “single_cat_title”?

    Many thanks.

    You have is_tax(‘media_type’), but it’s buggy.

    Don’t know about single_cat_title(). I guess you could copy it and replace ‘category’ with your own taxonomy.

    You should already be able to query for taxonomy and term… (re-enable the rewrite option).

    Query for a category, locahost/category/term

    Where term would be a category name…. categories are also taxonomies, so you can use them for testing purposes using the format above, or in the non-pretty format..


    There is an is_taxonomy function, you could check like so..

    $taxonomy = get_query_var('taxonomy') ? get_query_var('taxonomy') : 0;
    if(is_taxonomy($taxonomy)) {

    Thanks to the advice, I’m nearly there. Both is_tax() and is_taxonomy($taxonomy) seem to work, in that they return the title as “posts tagged with” (don’t read too much into the word “tagged” here, I just haven’t changed it from the default text that came with my theme yet). So, it seems to recognize the “elseif (is_taxonomy($taxonomy)) bit. I just need to figure out what to define $archive_display_name for that statement.

    In other words:

    if (is_category()) $archive_display_name = single_cat_title('', false);
    elseif (is_tag()) $archive_display_name = single_tag_title('', false);
    elseif (is_taxonomy($taxonomy)) $archive_display_name = ????????????;
    ..... etc.

    I know it must be possible, because the correct term is used for the page title. I just have not yet been able to figure out how…

    Hey…. I think I did it! Thanks to the advice from t31os_ and going back to Justin Tadlock’s page and comparing notes, I rewrote the line as:

    elseif (is_taxonomy($taxonomy)) $archive_display_name = get_query_var( 'term' );

    And it ****** worked. I’m so excited right now. Any problems with that approach? Seems like it works, so I’m going with it for now. Thanks for the help!

    Looks ok to me…. 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display posts with the same custom field value as the current post’ is closed to new replies.