Custom Post Types Filters

Quick Guide to WordPress Custom Post Types Management and Filter Programming

This document explains how to create, manage and filter by the most common attribute types (taxonomies) any custom created post type for Wordpress . You will need a little PHP programming knowledge to create some specific templates with some custom filters and loops.

After searching and trying several custom post types plug-ins, the best I can recommend, by far, is the Custom Content Type Manager (CCTM) available for free:

https://wordpress.org/plugins/custom-content-type-manager/

In this post I will show the basic usage of this plug-in and then how to create some templates with custom and complex filters for custom post listing and loops using PHP and WordPress functions.

PART 1: Creating Custom Post Types using the plug-in:

I will be referring to Custom Post Types as CPT from now on.

Once you have installed the plugin, go to “Custom Content Types” in the administrator panel’s left menu, and create your desired CPT. Then you can create the attributes’ names and types for this new CPT by clicking the “Manage custom fields” link on the list, as it is shown on figure 01. Then select the “Custom fields” tab above and click “create custom field” button to start.

figure01

 

In order for the created CPT to appear in the WordPress administrator menu, you must activate them first (see figure 01). You can find the “activate” link under your CPT name in the list.

One of the things I love the most about this plug-in is the availability and support for almost any attribute type you can imagine; and the way it works with different data types. Below you can find a list of all possible attribute types.

figure02

 

You can also create validation rules, CSS classes, styles and output filters for each field in the following page, but as this is a quick guide, I won’t spend time explaining this since it is very clear and well explained in the plug-in’s documentation.

For filtering examples purposes, I have created four fields for my CPT: two text fields (one string and another numeric), a dropdown containing different option values and a checkbox (binary)

figure03

And that’s it! Now you are ready to create templates with lists and post details for your CPTs.

 

PART 2: Creating templates with custom filters:

 

1 – Creating a template to list CPTs with filters:

This is just one way for doing this, there are some other possibilities for achieving the same results. First of all we need to create a new template file inside our current WordPress theme (you can also create a new folder inside your theme root folder; let’s say “custom-templates” and they will be automatically loaded by WordPress too.

In order to let WordPress know this is a template file, we just need to put the following commented code line in the top part of the file:

 

I will now use WordPress WP_Query() function to load my recently created CPTs in my template file:

This file loads a complete list of my CPT sorted by the custom field “BRAND” and with a “Published” status.

2– Multiple Meta Entries, a little bit more complex filters…
  • Only Beretta guns:

  • Only guns with price over and equal 750 USD

  • Only Glock guns with price lower than 800 USD

 

  • All CZ guns OR any Beretta suitable for concealed carry AND price lower than 400 US

  • All gun models containing the word “ber” in they brand name

 

IMPORTANT:

“compare” parameter/operator will accept the following values:

‘=’,’!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’,’BETWEEN’, ‘NOT BETWEEN’, ‘REGEXP’, ‘NOT REGEXP’, or ‘RLIKE’.

Default is ‘IN’ when “value” is an array, ‘=’ otherwise.

 

“type” parameter/operator will accept the following values:

‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’,’DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, or ‘UNSIGNED’.

Default is ‘CHAR’.

 

“relation” parameter/operator will accept the following values:

‘AND’, or ‘OR’.

Default is ‘AND’.

3 – Template for showing a single CPT in website’s frontend (Post detail page)

If you create a file starting with “single-“ and followed by the name of your already created CPT and save it inside your active theme root folder, all post of this type will be automatically loaded and shown using this template. So for this example I have created a file named single-gun.php:

This template can be copy-pasted from the “Custom Content Types” list in your WordPress administrator’s panel. (see figure below)

figure04

 

CCTM  and CPT F.A.Q.s

1 – Q: Pagination doesn’t work with Custom Post Types using “/%postname%/”permalink structure.

A: You must activate this option from the “Custom Content Types” menu on your WordPress administration panel’s left menu, then click on the “edit” link next to the name of your CPT, go to the “URLs” tab on the top (see figure 04 below) and select “/%postname%/” on the “Permalink Action” dropdown. Then save changes and you’re done!

figure05

Still not working?

Go to WordPress settings page, under “permalinks” tab, select “default” option for links structures (http://mywordpress-site/?p=123) and save changes. Then select the desired permalink structure, in this case “http://mywordpress-site/sample-post/” and save changes again. This will refresh all existing posts structure.

 

2- Q: After having moved my WordPress site to another server, custom post types pages in administrator’s panel are not showing up anymore (Post lists or edit/details pages).

A:  You must clean CCTM cache from CCTM settings. Go to “Custom Content Types”->”Clear Cache”

 

3 – Q: After having moved my WordPress site to another server, created custom post types and/or attributes are not showing in administrator’s panel.

A: You must import your Custom Post Types, previously exported from your website in jSON format on your previous server.

For export definition: go to “Custom Content Types”->”export Definition”

Make sure the created/downloaded file has the following extension “.cctm.json”. Sometimes you have to change it manually from the file manager in your computer.

For import definition: go to “Custom Content Types”->”import Definition”.

Sometimes you need to clear cache as it shown on F.A.Q.#2 above after importing definitions for CPT to show on WordPress administrator’s menu.

 

Sources:

CCTP plug-in documentation:

https://wordpress.org/plugins/custom-content-type-manager/

 

WordPress query and post functions documentation:

https://codex.wordpress.org/Class_Reference/WP_Query

https://codex.wordpress.org/Class_Reference/WP_Meta_Query

https://codex.wordpress.org/Function_Reference/query_posts