• Resolved mending

    (@mending)


    Hi, I’m using this to display related posts by tags, but need to exclude tags that are years (2014, 2013, 2012, etc. or any numbers at all). Is that possible?

    $args=array(
     'tag__in' => $tag_ids,
     'post__not_in' => array($post->ID),
     'posts_per_page'=> 5, // Number of related posts that will be shown.
     'ignore_sticky_posts'=>1
Viewing 2 replies - 1 through 2 (of 2 total)
  • You can write a conditional and use regex to check for the year format in the tag, but without much more information or code to work off it would be rather hard to provide support on the subject.

    Regex for matching a valid year : ^\d{4}$

    reference : http://stackoverflow.com/questions/4374185/regular-expression-match-to-test-for-a-valid-year

    Thread Starter mending

    (@mending)

    Hi, thanks! How would you implement that into this? (my complete “single-related.php” page)

    <?php
    $orig_post = $post;
    global $post;
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    	$tag_ids = array();
        foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    
        $args=array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'posts_per_page'=> 5, // Number of related posts that will be shown.
        'ignore_sticky_posts'=>1
        );
    
        $my_query = new wp_query( $args );
    
    	if( $my_query->have_posts() ) { ?>
    
         <section id="related-posts" class="clearfix">
         <h3 id="related-posts-title"><?php _e("Related Articles", "framework"); ?></h3>
         	<ul class="clearfix"><?php
    
        while( $my_query->have_posts() ) {
        	$my_query->the_post();
    
    		// Set search result class
    		if ( has_post_format( 'video' )) {
    		$st_search_class = 'video';
    		} else {
    		$st_search_class = 'standard';
    		}
    		?>
    
    		<li class="<?php echo $st_search_class ?>">
            <h4 class="entry-title"><a href="<?php the_permalink()?>" rel="bookmark" title="<?php echo esc_attr( sprintf( the_title_attribute( 'echo=0' ) ) ); ?>"><?php the_title(); ?></a></h4>
            </li>
    
    <?php } ?>
    </ul></section>
    <?php }
        }
     $post = $orig_post;
    wp_reset_query();
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude tags that are years (numbers)’ is closed to new replies.