Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Load FacetWP facets programmatically from a file

Stanislav KhromovStanislav Khromov

Did you know that you can load FacetWP facets from a file instead of from the database? This way you can check them into version control. It works similar to the Advanced Custom Fields Local JSON feature!

Here’s how you accomplish it:

In your themes functions.php

/**
 * FacetWP facets
 */
add_filter( 'facetwp_facets', function($facets) {

    $imported_facets = json_decode(file_get_contents(trailingslashit(dirname(__FILE__)) . 'facetwp/facets.json'), true);

    if(!isset($imported_facets['facets'])) {
        return $facets;
    }

    foreach($imported_facets['facets'] as $single_facet) {
        $facets[] = $single_facet;
    }

    return $facets;
});

In the facetwp/facets.json you would put the FacetWP JSON export code, for example:

{
  "facets": [
    {
      "label": "Search",
      "name": "search",
      "type": "search",
      "search_engine": "relevanssi",
      "placeholder": "Enter search terms"
    },
    {
      "label":"Author",
      "name":"author",
      "type":"fselect",
      "source":"post_author",
      "multiple":"no",
      "label_any":"Any",
      "parent_term":"",
      "orderby":"count",
      "operator":"and",
      "count":"1000"
    }
 ]
}

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 0
There are currently no comments.