This add-on lets you create facets based on (meta-based) custom fields made with the Pods plugin. From version 1.2.3, you can also use Pods custom fields in FacetWP’s Listing Builder and in Sort facets.

This add-on requires Pods version 2.7.9+.

Usage

Browse to Settings > FacetWP and create a new facet.

In the facet’s “Data source” dropdown, you’ll see a “Pods” header with all the fields you’ve created. Just choose one of these Pods fields.

To use Pods fields in the Listing Builder, in the “Display” tab, select the field from the Listing Builder’s item selection dropdown when adding a new item. In the “Query” tab, Pods fields can be chosen in the “Sort by” or “Narrow results by” dropdowns.

In a Sort facets “Sort options” setting, Pods custom fields can be selected as fields to sort by.

Using a Pods template

Browse to Settings > FacetWP > Templates and create a new FacetWP listing template.

When on the “Display” tab, click the “Switch to advanced mode” link on the right side. Here’s an example of what to use for the Display Code box:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

<?php while ( have_posts() ): the_post(); ?> <?php echo pods( 'your_pod_name', get_the_id() )->template( 'Your Pods Template Name' ); ?> <?php endwhile; ?>

Finally, click on the “Query” tab to define which post type(s) to fetch, and other query-related settings.

Using taxonomy term custom fields as facet data source

With Pods, you can add custom fields to taxonomy terms. When you set such a custom term field as the data source in a facet, you will notice that the facet does not display any choices. This is because custom fields attached to taxonomy terms cannot be indexed directly by FacetWP.

However, with a bit of custom code, using a custom term field as data source is possible. See this section on the Advanced Custom Fields page. The instructions and custom code for Pods are exactly the same.

Using Pods Relationship fields as facet data source

A Pods Relationship field as facet source. Choose the field under the Pods heading.
A Pods Relationship field as facet source. In this example, the chosen field “Related products” is from the “Post” pod. Choose the field under the Pods heading.
A Pods Relationship field Related products on the Post pod, set to the product post type.
A Pods Relationship field “Related products” on the “Post” pod, set to the “product” post type.

Pods Relationship fields can be used as the data source of facets. Bi-directional relationship fields will work too. Just make sure to select the Relationship field that is under the “Pods” heading in the facet’s dropdown (not the ones with the same name under the “Custom fields” heading).

If the related field type is a (custom) post type, the facet will automatically index the related posts’ id’s as facet_value (which will be visible in the URL) and the related posts’ post titles as facet_display_value (which will be visible as facet choices).

If you want your facet to display the content of a custom field of the related post instead of its post title, add the following code to your (child) theme’s functions.php, and adapt it as needed.

In this example, the related post id (in $related_post_id) is used with get_post_meta() to get the content of a custom field of that related post type. Make sure to replace field_name with the name of your custom field.

You could also use $related_post_id with other WordPress functions to get whatever info you need from the post. If the custom field returns an array instead of a simple text string, the code will need to be adapted.

Make sure to re-index after adding this code or making changes to it.

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_index_row', function( $params, $class ) { if ( 'my_facet_name' == $params['facet_name'] ) { // Change 'my_facet_name' to the name of your facet that has the (bidirectional) relationship field as data source. $related_post_id = $params['facet_value']; // Depending on how your relationship field saves its data this might need to be changed or looked up from a different value. if ( ! empty( $related_post_id ) ) { $params['facet_display_value'] = get_post_meta( $related_post_id, 'field_name', true ); // Lookup the custom field. Change 'field_name' to your custom field name. This would need adjusting if it returns an array rather than text string. $params['facet_value'] = $params['facet_display_value']; } else { $params['facet_value'] = ''; // Don't index this row. } } return $params; }, 10, 2 );

Caching and the Pods plugin

If you are looking into increasing the loading speed of your (facet) pages and you are using Pods, experiment with the Pods Alternative Cache add-on plugin, especially if you are hosting your site on WP Engine.

Pods Alternative Cache is a good alternative for Pods’ built-in cache if your site already uses object caching. It provides better performance of Pods sites on hosting plans where object cache sizes are limited or object caching is unavailable. It was developed for and tested on the WP Engine platform to improve the performance of cached objects generated from Pods, but it works on other hosting providers too.

For large Pods installations with hundreds of Pods and fields, having them all stored in the object cache can cause the object cache itself to become ineffective because it will constantly run out of space and trash Pods objects (or other useful cached objects from WordPress, the theme, or plugins). The add-on allows Pods to use more consistent persistent caching without affecting other plugins and WordPress caching objects.

More info about Pods Alternative Cache add-on can be found here.

For even further optimization of Pods on your site, you could look into leveraging Pods Alternative Cache by using partial page caching.

Registering new Custom Post Types

If you add new Custom Post Types with Pods, keep an eye on the following settings in the Advanced Options tab:


Pods - register custom post types settings.
Pods – register custom post types settings.

Changelog

1.2.4

  • Fixed indexing issue for certain Pods pick fields (single-select taxonomy sources)

1.2.3

  • Fixed display all Pods fields (for use within the Layout Builder and/or Query builder)

1.2.2

  • Fixed issue with pick (relationship) fields that aren't predefined lists

See also

Last updated: November 22, 2024