How to create archive page for custom post type filtered by meta value

What if you need to create an archive page for a custom post type where you display posts filtered by some criteria like post owner or meta field? For example, you have a rental property listings on the website and … Continued

What if you need to create an archive page for a custom post type where you display posts filtered by some criteria like post owner or meta field? For example, you have a rental property listings on the website and creating a page where you display all properties for a specific owner. Let’s see how to do that!

Make sure you have a good search filter

First of all, let’s look at your search criteria. While we can use custom meta fields for the filtering, it’s not the best option. Problem is that WordPress stores all posts’ custom fields in one huge table, which is not indexed and once you have a lot of posts on your website, it’s going to slow page loading time down a lot! It’s better to use one of the “indexed” criteria for filtering posts, such as post owner (user), taxonomy (tag, category or any custom taxonomy), post parent id. In case of rental property listings, I would go with post owner, to implement that do the following:

  • Create a user for each property owner. Use custom user role or give them some lower level access role like contributor.
  • Change author of your custom post type to one of the property owner users.

Now, you have all your custom posts for property listings owned by a different users. This will make filtering more efficient and allows to utilize WordPress inner mechanics to control user’s access to listings.

In case you want to filter listings by some other criteria, say location or property features – use custom taxonomy, that’s basically custom category or tags you can add to your custom post type. For example, you could have taxonomy for location (with state on top level and cities as a sub-categories) or for the property type (House, Apartment). Non hierarchical taxonomies (aka tags) make sense for things like listing amenities or features (Fireplace, Pool, etc).

Keep in mind – you can have multiple custom taxonomies added to your post, so to make your life easier, don’t mix location and home features in one, having two separate taxonomies will make more sense and keep things under control.

Custom archive page

WordPress has a built in functionality to display listings by category, tag or custom taxonomy, so if, for example, you used a custom taxonomy for property type and now need to show listings belonging to a specific type – you are lucky, WordPress has all heavy lifting done for you. Just make sure your custom taxonomy has am archive page enabled and style that page. For example, if custom taxonomy slug is “type” (for property types) and value you want to filter by is House, your archive page’s URL will be something like www.yoursite.com/type/house/.

To customize that page you can make a copy of category.php (archive.php or index.php), save it as archive-type.php (where “type” is your taxonomy’s slug) and edit the new file to fit your needs for the displaying listings.

Really custom search filter

But what if you can not use taxonomies, for example you want to show all listings which belong to a specific owner? Or, maybe, you want to show all listings which belong to user who is currently logged in? May be you want to go one step further and have a page which will fetch listings using multiple criteria, for example owner and category.

In this case, you can do this with a custom page template.

Start by creating a custom page template:

  • Copy page.php to page-my-listings.php (file name can be anything, I prefer to prefix page templates with “page-“)
  • Add a template name, so WordPress will recognize this as a page template. In the PHP file add the following at the top of the file:
    <?php
    /*
    * Template Name: My Listings
    */
    ?>
  • Create a new page in WordPress admin and select “My Listings” from template list on the right hand side. You can enter some content which will be shown on the page, or just leave it blank. Does not matter.

Now, let’s edit the page template file to display your custom posts.

Open the file in the editor and locate the spot where you want to show listings. Look for “the_content()” or something like that – usually it’s a good spot to add your custom content. If not sure – experiment, move things around 🙂

Here is a sample code for displaying custom post type “listing” filtered by owner, as an owner we will use logged in user. So, basically it’s a “My Listings” page where user can see rental properties they own. We are creating a custom WordPress query and using the loop to go through the results, see manual page on WP_Query for more details.

<?php
// get current user's ID first
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
    _e( 'Please log in to see your listings' ); ?>
} else {
    // Logged in, let's setup our custom search criteria
   $args = array(
       'author' => $current_user->ID, // filter by logged in user as post author
       'post_type' => 'listing', // custom post type, I use 'listings'
       'post_status' => 'publish', // this will only show published posts, change this if you want all
   );

   // execute the query!
   $the_query = new WP_Query( $args ); 

   // now loop through all results and display them
?>

<?php if ( $the_query->have_posts() ) : ?>
    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <!-- show custom fields, pull images, etc here -->
    <?php endwhile; ?>
    <!-- end of the loop -->
    <?php wp_reset_postdata(); ?>
<?php else : ?>
    <p><?php _e( 'Sorry, no listings.' ); ?></p>
<?php endif; ?>

That’s it, you have your listings page!

Leave a Reply

Learning WordPress development?

Subscribe and never miss a post about WordPress themes and plugins coding techniques, WordPress website development workflow and best practices.