Advertisement
Theme Development

Creating a Custom 'The Posts I Commented On' Loop

by

Sometimes a WordPress developer might need custom loops which can not be generated with standard loops as category, author, index, date, archive, taxonomy etc. One of them is "the posts which I commented on". In Q&A sites it means "the questions I answered", so it might be needed for many developers. On base of this tutorial we can create another custom loops. Let's go to create this section.


Step 1

Go to your theme folder and create a myanswers.php file, then copy and paste following code there:

<?php /* Template Name: myanswers */ ?>
<?php
get_header(); ?>
<?php
get_template_part( 'loop', 'myanswers' );	 
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

We have just created template file called myanswers. It will be used for displaying our custom loop.


Step 2

Stay in your theme folder and create second file called loop-myanswers.php. And paste following code into that file:

<?php
if($wp_query->query_vars['paged']==0){$wp_query->query_vars['paged']=1;}
$querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts  inner join $wpdb->comments on $wpdb->posts.ID=$wpdb->comments.comment_post_ID 
    WHERE $wpdb->posts.post_status='publish' and $wpdb->comments.user_id=".wp_get_current_user()->ID."
    GROUP BY $wpdb->posts.ID
    ORDER BY $wpdb->posts.post_date DESC  ";

$lim_per_page=" limit ".($wp_query->query_vars["posts_per_page"]*($wp_query->query_vars['paged']-1)).",".$wp_query->query_vars["posts_per_page"];

$query_for_count = $wpdb->get_results($querystr, OBJECT);
$wp_query->max_num_pages=ceil($wpdb->num_rows/$wp_query->query_vars["posts_per_page"]);
$querystr=$querystr.$lim_per_page;
$pageposts = $wpdb->get_results($querystr, OBJECT);
 ?>

<?php if ($pageposts): ?>
 <?php global $post; 
 ?>
 
 <?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>
 
<?php
/// THIS PART IS LOOP FROM TWENTYTEN, YOU CAN CHANGE IT HOWEVER YOU WANT BEGIN
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), 
the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

			<div class="entry-meta">
				<?php twentyten_posted_on(); ?>
			</div><!-- .entry-meta -->

	<?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
			<div class="entry-summary">
				<?php the_excerpt(); ?>
			</div><!-- .entry-summary -->
	<?php else : ?>
			<div class="entry-content">
				<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) ); ?>
				<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
			</div><!-- .entry-content -->
	<?php endif; ?>

			<div class="entry-utility">
				<?php if ( count( get_the_category() ) ) : ?>
					<span class="cat-links">
						<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
					</span>
					<span class="meta-sep">|</span>
				<?php endif; ?>
				<?php
					$tags_list = get_the_tag_list( '', ', ' );
					if ( $tags_list ):
				?>
					<span class="tag-links">
						<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
					</span>
					<span class="meta-sep">|</span>
				<?php endif; ?>
				<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></span>
				<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
			</div><!-- .entry-utility -->
		</div><!-- #post-## -->

 <?php endforeach; 
/// THIS PART IS LOOP FROM TWENTYTEN, YOU CAN CHANGE IT HOWEVER YOU WANT  END
?>
 <?php else : ?>
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php include (TEMPLATEPATH . "/search.php"); ?>
 <?php endif; ?>
 
 <?php
echo  $wp_query->max_num_pages;
 if (  $wp_query->max_num_pages > 1 ) : ?>
				<div id="nav-below" class="navigation">
					<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', 'twentyten' ) ); ?></div>
					<div class="nav-next"><?php previous_posts_link( __( 'Next<span class="meta-nav">→</span>', 'twentyten' ) ); ?></div>
				</div><!-- #nav-below -->
<?php endif; ?>

This file is for generating our custom loop in template file.


Step 3

Open theme functions file of your theme(functions.php) and add this function and filter to that file:

add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
/* Plugin Name: Parameter
Plugin URI: http://webopius.com/
Description: A plugin to allow parameters to be passed in the URL and recognized by WordPress
Author: Adam Boyse
Version: 1.0
Author URI: http://www.webopius.com/
*/
$qvars[] = 'paged';
return $qvars;
}

This couple of function and filter is for getting page id which is needed for building pagination in permalink-structured sites.


Step 4

At last, go to your Dashboard, create new page from Pages->Add new and name it "myanswers", by default its slug will be myanswers. Before publishing select template for this page. In template widget you will see combobox which contains myanswers option. Select it.


After selecting myanswers option click to Publish button.


Done!

Now you can use yoursite.com/myanswers url as page which displays the loop of "the posts you commented on". And of course not only you, every logged in user can see their own one.

Related Posts
  • Code
    Creative Coding
    Advanced Use of Attachments in WordPress: Creating Custom Queries for AttachmentsAdvanced use of attachments in wordpress 400
    This tutorial is the second in a four part series in which you'll learn some techniques for working with images in attachments in WordPress which give you advanced options.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme from Static HTML: Creating an Archive TemplateCreating wordpress theme from html 400
    If you've been working your way through this series, you now have a functioning theme with two page templates. The steps I've demonstrated to this point are: preparing your markup for WordPress converting your HTML to PHP and splitting your file into template files editing the stylesheet and uploading your theme to WordPress adding a loop to your index file adding meta tags, the wp_head hook and the site title and description to your header file adding a navigation menu adding widget areas to the header and sidebar adding widget areas, a colophon and the wp_footer hook to the footer file creating template files for static pages. Read More…
  • Code
    Theme Development
    Creating a WordPress Theme from Static HTML - Creating a Page TemplateCreating wordpress theme from html 400
    So far in this series, I've shown you how to create a fully functioning WordPress theme from static HTML. We've covered the following steps: preparing your markup for WordPress converting your HTML to PHP and splitting your file into template files editing the stylesheet and uploading your theme to WordPress adding a loop to your index file adding meta tags, the wp_head hook and the site title and description to your header file adding a navigation menu adding widget areas to the header and sidebar adding widget areas, a colophon and the wp_footer hook to the footer file. At the moment, your theme only has one template file for displaying content—the index.php file. A powerful feature of WordPress is the ability to use template files for different kinds of content.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme From Static HTML: Adding a LoopCreating wordpress theme from html 400
    In the first three parts of this series, you learned how to prepare static HTML for WordPress and to create a theme by splitting your HTML file into a set of template files and editing the stylesheet. You then uploaded your theme to WordPress and activated it. The theme still isn't displaying any content you add via the WordPress admin however; to do that you need to add a loop to your template files.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme From Static HTML: Creating Template FilesCreating wordpress theme from html 400
    In the first part of this series, I showed you how to prepare your HTML and CSS files for WordPress, ensuring the structure would work, the code was valid and that the correct classes were being used. In this tutorial you'll learn how to take your index.html file and split it up into a set of template files for use by WordPress.Read More…
  • Code
    Plugins
    Creating Client Testimonials With Custom Post TypesCreating client testimonials with custom post types
    If you're running any type of business online, it's a good idea to get your clients' opinions on what they thought about the services you provided. First of all, this can benefit you by giving you feedback on how you can improve aspects of your business, but most of all, it can give you great testimonials, which can help to persuade potential clients to use your services.Read More…