WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display posts with the same custom field value as the current post (10 posts)

  1. tgildred
    Member
    Posted 4 years ago #

    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 ">
    						<?php
    							$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); } ?>
    					</p>
                    <div class="bottom"></div>
                    </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 ?>
    					</div>
    					<?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!

    Thanks.

  2. MichaelH
    Member
    Posted 4 years ago #

    Might want to see if this plugins will help:

    http://wordpress.org/extend/plugins/custom-field-taxonomies/

  3. jvinch
    Member
    Posted 4 years ago #

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

    http://justintadlock.com/archives/2009/05/06/custom-taxonomies-in-wordpress-28

    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.

  4. tgildred
    Member
    Posted 4 years ago #

    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:

    <?php
    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. :)

  5. tgildred
    Member
    Posted 4 years ago #

    Small update to my last post: I changed

    'rewrite' => true

    to

    '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.

  6. scribu
    Member
    Posted 4 years ago #

    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.

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

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

    Example:
    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..

    localhost/?taxonomy=category&term=CATNAMEHERE

    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)) {
    // DO WHATEVER
    }
  8. tgildred
    Member
    Posted 4 years ago #

    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...

  9. tgildred
    Member
    Posted 4 years ago #

    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!

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    Looks ok to me.... :)

Topic Closed

This topic has been closed to new replies.

About this Topic