Tag Archives: post

Heres a little WordPress snippet that allows you to exclude the current WordPress post from the WP Query – i.e. list of recent posts, more posts from the same category, etc. What the snippet does, is getting the ID of the current post and then use this post ID to exclude the current post form being included in the query.

Instructions
Add this code to your WordPress theme template files where you usually use your queries.

<?php
     $currentID = get_the_ID(); // get current post ID and convert it into a variable
     $my_query = new WP_Query(
          array('cat' => '1',
          'showposts' => '5',
          'post__not_in' => array(
               $currentID))
          );
     while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
          <h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
          <?php the_content(); ?>
     <?php endwhile; ?>

This WordPress snippet will let you show a list of posts by a specific user. This is useful if you, for example, want to show a list of posts from a specific user on Custom Post Types, etc.

Instructions
1. Add a Custom Field (read more about WordPress Custom Fields) to the Page or Custom Post Type etc. In that custom field add “user-id” as the name and the numeric value “1” (or whatever the ID of the specific user is) as the Value“.

2. Add the following to your template.php file

<?php $the_user_id = get_post_meta( $post->ID, 'user-id', true); if ($the_user_id) { ?>
     <?php query_posts("author=$the_user_id&posts_per_page=5" );?>
          <ul>
               <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><br><?php the_excerpt() ?></li>      
               <?php endwhile; else: ?>
          </ul>
          <p><?php _e('No posts by this user-id'); ?></p>
          <?php endif; ?>
     <?php wp_reset_query(); ?>
<?php } else { ?>
     <!-- No value ha been set for the Custom Field "user-id" -->
<?php } ?>

This snippet automatically sets the featured image by fetching the first image of the WordPress post.
*Note – if you choose a featured image, that will be displayed instead.

Instructions:
Add this to the functions.php file

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           }
                        }
      }
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');

Related links:
http://wpforce.com/automatically-set-the-featured-image-in-wordpress/

Heres a little snippet that will display recent posts from a specific category in WordPress

<?php
     $catquery = new WP_Query( 'cat=3&posts_per_page=10' );
     while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
     <li>
          <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
          <ul>
               <li><?php the_content(); ?></li>
          </ul>
     </li>
</ul>
<?php endwhile; ?>

Heres a little trick on how to show a list of the most popular WordPress post by post read count – without using a plugin.

Instructions
Add this code to your functions.php file.

function count_post_visits() {
	if( is_single() ) {
		global $post;
		$views = get_post_meta( $post->ID, 'my_post_viewed', true );
		if( $views == '' ) {
			update_post_meta( $post->ID, 'my_post_viewed', '1' );	
		} else {
			$views_no = intval( $views );
			update_post_meta( $post->ID, 'my_post_viewed', ++$views_no );
		}
	}
}
add_action( 'wp_head', 'count_post_visits' );

Then add this anywhere in your template where you wish to display the posts.

<?php
$popular_posts_args = array(
	'posts_per_page' => 3,
	'meta_key' => 'my_post_viewed',
	'orderby' => 'meta_value_num',
	'order'=> 'DESC'
);
 
$popular_posts_loop = new WP_Query( $popular_posts_args );
 
while( $popular_posts_loop->have_posts() ):
	$popular_posts_loop->the_post();
	// Loop continues
endwhile;
wp_reset_query();
?>