1. Code
  2. Theme Development

Creating a Custom 'The Posts I Commented On' Loop

Read Time:4 minsLanguages:

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:

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:

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:

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.


Now you can use 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.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.