- Help Center Home
- Getting started
- Introducing FacetWP
- Installation and updates
- FAQ
- How it works
- What are facets?
- Facet types
- Built-in facet types
- Checkboxes
- Dropdown
- Radio
- fSelect
- Hierarchy
- Slider
- Search
- Autocomplete
- Date Range
- Number Range
- Star Rating
- Proximity
- Pager + load more
- Sort
- Reset
- User Selections
- Add-on facet types
- Map
- Hierarchy Select
- Range List
- Time Since
- A-Z Listing
- Color
- Custom facet types
- Custom facet types
- Indexing
- Listing templates
- Extras & integrations
- Add-on features and extras
- Using FacetWP with …
- Built-in integrations
- Advanced Custom Fields
- WooCommerce
- SearchWP
- WP-CLI
- Add-on integrations
- Blocks
- Bricks
- Elementor
- Beaver Builder
- WP Recipe Maker and Tasty Recipes
- Relevanssi
- WPML and Polylang
- Meta Box
- Flatsome (theme)
- External integrations
- Breakdance
- Document Library Pro
- Listify (theme)
- Listable (theme)
- WPGraphQL
- Tips & tricks
- WordPress multi-site
- WP All Import
- WebToffee Import Export
- WP Job Manager
- Easy Digital Downloads
- EDD Reviews
- Intuitive Custom Post Order
- Custom Taxonomy Order
- Post Types Order
- Genesis framework
- WP External Links
- ElasticPress
- Yoast SEO
- All in One SEO (Pro)
- The Events Calendar Pro
- Google Analytics 4
- Image Optimization by Optimole
- Meow Lightbox
- Cookiebot
- Caching, hosting & security
- Object caching
- WP Rocket
- Cloudflare
- WP Engine
- Pressable
- New Relic
- WordPress REST API Authentication
- All-In-One Security (AIOS)
- Fast Velocity Minify
- Incompatibilities
- Incompatible plugins and themes
- Troubleshooting
- Troubleshooting guide
- Using the right query
- Common issues
- Common indexing issues
- Get support
- Developers
- Hooks reference
- Indexing hooks
- Querying hooks
- Output hooks
- facetwp_facet_display_value
- facetwp_facet_html
- facetwp_facet_render_args
- facetwp_facet_pager_link
- facetwp_facet_sort_options
- facetwp_template_html
- facetwp_shortcode_html
- facetwp_render_params
- facetwp_render_output
- facetwp_builder_item_value
- facetwp_builder_dynamic_tags
- facetwp_builder_dynamic_tag_value
- Advanced hooks
- Deprecated hooks
- JavaScript reference
- Shortcodes reference
- FacetWP REST API
- How FacetWP works
- The FacetWP URL
- FacetWP speed and limits
- Tutorials
- Code snippets
- Feedback
- What’s new
- Changelog
- News & announcements
How to preserve facet selections across pages
FacetWP automatically generates a permalink URL whenever you interact with facets. This allows you to bookmark the page with the facets in place.
However, when navigating to other pages on your site, the facet selections are lost. Fortunately, it’s possible to preserve the facets’ state across pages, by using cookies.
The following code preserves the facet choices in a cookie, and upon returning to the page, will reload the page with the active facet choices stored in the cookie.
Note:The code below is meant to work for only one page with facets. If you have multiple pages, the reloading of the page with the facet choices stored in the cookie will happen on all pages with facets, not only the one where on which the choices were made. To prevent this, you can wrap the whole function in a condition that checks the URI of the page with
FWP_HTTP.uri
. (The URI is the part of the page URL that comes after the domain name, without a /
at the beginning or end).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_action( 'wp_head', function() { ?> <script> (function($) { $(function() { // Optionally use the code only on a specific page, by checking the page URI // Only needed if you have multiple pages with facets if ( 'my/facetpage_uri' == FWP_HTTP.uri ) { // Replace 'my/facetpage_uri' with your page URI // After FacetWP reloads, store any updates into a cookie */ $(document).on('facetwp-loaded', function() { var date = new Date(); var facets = window.location.search; date.setTime(date.getTime() + (24 * 60 * 60 * 1000)); document.cookie = "facetdata=" + facets + "; expires=" + date.toGMTString() + "; path=/"; }); // When FacetWP first initializes, look for the "facetdata" cookie // If it exists, set window.location.search= facetdata $(document).on('facetwp-refresh', function() { if (!FWP.loaded) { var facets = window.location.search; var facetdata = readCookie('facetdata'); if (null != facetdata && '' == facets && '' != facetdata && facets != facetdata) { document.cookie = 'facetdata=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/'; window.location.search = facetdata; } } }); // Cookie handler function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } } }); })(jQuery); </script> <?php }, 100 );
See also
Last updated: September 10, 2024