/facets
This filter is called when fetching facets in the admin interface and on the frontend to display facets and query content.
It allows to add your own facets with your own logic. Creating custom facets requires advanced knowledge in PHP and SQL languages
Argument | Type | Description |
$facets | array | Holds registered facet arguments |
[pastacode lang=”php” manual=”function%20prefix_register_facet(%20%24facets%20)%20%7B%0A%0A%2F%2F%20’my_facet’%20corresponds%20to%20the%20facet%20slug.%0A%24facets%5B’my_facet’%5D%20%3D%20%5B%0A’name’%20%3D%3E%20__(%20’My%20Facet’%2C%20’text-dmain’%20)%2C%20%0A’type’%20%3D%3E%20’filter’%2C%0A’class’%20%3D%3E%20’My_Facet_Class’%2C%20%0A%5D%3B%20%0A%0Areturn%20%24facets%3B%0A%0A%7D%0A%0Aadd_filter(%20’wp_grid_builder%2Ffacets’%2C%20’prefix_register_facet’%2C%2010%2C%201%20)%3B” message=”” highlight=”” provider=”manual”/]
The plugin will use the following methods, if available, from your facet class
query_facet
: To query facet values/names depending of content and filter state.render_facet
: To display facet values/names queried byquery_facet
method.query_objects
: To filter content by querying object ids (post ids, term ids or user ids) from selected facet values.
As example, this facet class allows to create an alphabetical list to filter content by first letter:
[pastacode lang=”php” manual=”%2F**%20%0A*%20Alphabetical%20Facet%20class%20example%20%0A*%20%0A*%20%40class%20My_Facet_Class%20%0A*%20%40since%201.0.0%0A*%2F%0Aclass%20My_Facet_Class%20%7B%20%0A%0A%09%2F**%20%0A%09*%20Constructor%20%0A%09*%20%0A%09*%20%40access%20public%20%0A%09*%2F%0A%09public%20function%20__construct()%20%7B%7D%0A%0A%09%2F**%20%0A%09*%20Query%20facet%20choices%0A%09*%0A%09*%20%40access%20public%20%0A%09*%20%0A%09*%20%40param%20array%20%24facet%20Holds%20facet%20settings.%20%0A%09*%20%40return%20array%20Holds%20facet%20items.%20%0A%09*%2F%20%0A%09public%20function%20query_facet(%20%24facet%20)%20%7B%0A%09%09%0A%09%09global%20%24wpdb%3B%20%0A%09%09%0A%09%09%24where_clause%20%3D%20wpgb_get_where_clause(%20%24facet%20)%3B%20%0A%09%0A%09%09%2F%2F%20We%20query%20facet_name%20distinctly%20by%20the%20first%20letter.%0A%09%09%2F%2F%20In%20this%20example%2C%20we%20do%20not%20use%20the%20facet_value%20because%20depending%20of%20its%20length%20it%20may%20be%20encoded%20(md5).%0A%09%09%24facet_names%20%3D%20%24wpdb-%3Eget_col(%0A%09%09%09%24wpdb-%3Eprepare(%20%0A%09%09%09%09%22SELECT%20DISTINCT%20UPPER(LEFT(facet_name%2C%201))%20AS%20letter%20%0A%09%09%09%09FROM%20%7B%24wpdb-%3Eprefix%7Dwpgb_index%20%0A%09%09%09%09WHERE%20slug%20%3D%20%25s%20%0A%09%09%09%09AND%20%24where_clause%0A%09%09%09%09GROUP%20BY%20letter%22%2C%0A%09%09%09%09%24facet%5B’slug’%5D%0A%09%09%09)%20%0A%09%09)%3B%20%2F%2F%20WPCS%3A%20unprepared%20SQL%20ok.%20%0A%0A%09%09return%20%24facet_names%3B%20%0A%09%7D%20%0A%09%0A%09%2F**%20%0A%09*Render%20facet%20choices%20%0A%09*%20%0A%09*%20%40access%20public%20%0A%09*%20%0A%09*%20%40param%20array%20%24facet%20Holds%20facet%20settings.%20%0A%09*%20%40param%20array%20%24items%20Holds%20facet%20items.%20%0A%09*%20%40return%20string%20Facet%20markup.%20%0A%09*%2F%0A%09public%20function%20render_facet(%20%24facet%2C%20%24items%20)%20%7B%0A%09%0A%09%09%24output%20%3D%20’%3Cdiv%20class%3D%22wpgb-alphabetical-facet%22%3E’%3B%0A%09%09%24output%20.%3D%20’%3Cul%20class%3D%22wpgb-inline-list%22%3E’%3B%0A%09%09%24output%20.%3D%20%24this-%3Erender_letters(%20%24facet%2C%20%24items%20)%3B%0A%09%09%24output%20.%3D%20’%3C%2Ful%3E’%3B%0A%09%09%24output%20.%3D%20’%3C%2Fdiv%3E’%3B%0A%09%0A%09return%20%24output%3B%0A%09%0A%7D%20%0A%0A%2F**%20%0A*%20Render%20letter%20buttons%20%0A*%20%0A*%20%40access%20public%20%0A*%20%0A*%20%40param%20array%20%24facet%20Holds%20facet%20settings.%20%0A*%20%40param%20array%20%24items%20Holds%20facet%20items.%20%0A*%20%40return%20string%20Letter%20buttons%20markup.%20%0A*%2F%0A%0Apublic%20function%20render_letters(%20%24facet%2C%20%24items%20)%20%7B%0A%0A%09%24output%20%3D%20”%3B%20%0A%09%24letters%20%3D%20str_split(%20’ABCDEFGHIJKLMNOPQRSTUVWXYZ’%20)%3B%0A%09%0A%09foreach%20(%20%24letters%20as%20%24letter%20)%20%7B%0A%09%09%24output%20.%3D%20’%3Cli%3E’%20.%20%24this-%3Erender_button(%20%24facet%2C%20%24items%2C%20%24letter%20)%20.%20’%3C%2Fli%3E’%3B%0A%09%7D%0A%0A%09return%20%24output%3B%20%0A%0A%7D%20%0A%0A%2F**%0A*%20Render%20letter%20button%20%0A*%0A*%20%40access%20public%0A*%20%0A*%20%40param%20array%20%24facet%20Holds%20facet%20settings.%0A*%20%40param%20array%20%24items%20Holds%20facet%20items.%0A*%20%40param%20string%20%24letter%20Current%20letter.%0A*%20%40return%20string%20Letter%20button%20markup.%0A*%2F%20%0A%0Apublic%20function%20render_button(%20%24facet%2C%20%24items%2C%20%24letter%20)%20%7B%0A%0A%09%24checked%20%3D%20in_array(%20%24letter%2C%20%24facet%5B’selected’%5D%2C%20true%20)%3B%0A%09%24disabled%20%3D%20!%20in_array(%20%24letter%2C%20(array)%20%24items%2C%20true%20)%3B%0A%09%24pressed%20%3D%20%24checked%20%3F%20’true’%20%3A%20’false’%3B%0A%09%24tabindex%20%3D%20%24disabled%20%3F%20-1%20%3A%200%3B%0A%0A%09%24output%20%3D%20’%3Cdiv%20class%3D%22wpgb-button%22%20role%3D%22button%22%20aria-pressed%3D%22’%20.%20esc_attr(%20%24pressed%20)%20.%20’%22%20tabindex%3D%22’%20.%20esc_attr(%20%24tabindex%20)%20.%20’%22%3E’%3B%0A%09%24output%20.%3D%20’%3Cinput%20type%3D%22hidden%22%20name%3D%22’%20.%20esc_attr(%20%24facet%5B’slug’%5D%20)%20.%20’%22%20value%3D%22’%20.%20esc_attr(%20%24letter%20)%20.%20’%22’%20.%20disabled(%20%24disabled%2C%20true%2C%20false%20)%20.%20’%3E’%3B%0A%09%24output%20.%3D%20’%3Cspan%20class%3D%22wpgb-button-label%22%3E’%20.%20esc_html(%20%24letter%20)%20.%20’%3C%2Fspan%3E’%3B%0A%09%24output%20.%3D%20’%3C%2Fdiv%3E’%3B%0A%0A%09return%20%24output%3B%0A%0A%7D%0A%0A%2F**%0A*%20Query%20object%20ids%20(post%2C%20user%2C%20term)%20for%20selected%20facet%20values%20%0A*%20%0A*%20%40since%201.0.0%20*%20%40access%20public%20%0A*%20%0A*%20%40param%20array%20%24facet%20Holds%20facet%20settings.%20%0A*%20%40return%20array%20Holds%20queried%20facet%20object%20ids.%20%0A*%2F%20%0A%0Apublic%20function%20query_objects(%20%24facet%20)%20%7B%0A%09%0A%09global%20%24wpdb%3B%0A%0A%09%24placeholders%20%3D%20rtrim(%20str_repeat(%20’%25s%2C’%2C%20count(%20%24facet%5B’selected’%5D%20)%20)%2C%20’%2C’%20)%3B%0A%09%2F%2F%20We%20query%20object%20ids%20that%20match%20selected%20letter(s)%20from%20index%20table.%0A%09%24object_ids%20%3D%20%24wpdb-%3Eget_col(%0A%09%09%24wpdb-%3Eprepare(%0A%09%09%09%22SELECT%20DISTINCT%20object_id%20%0A%09%09%09FROM%20%7B%24wpdb-%3Eprefix%7Dwpgb_index%0A%09%09%09WHERE%20slug%20%3D%20%25s%0A%09%09%09AND%20UPPER(LEFT(facet_name%2C%201))%20IN%20(%24placeholders)%22%2C%0A%09%09%09array_merge(%20(array)%20%24facet%5B’slug’%5D%2C%20%24facet%5B’selected’%5D%20)%0A%09%09)%0A%09)%3B%20%2F%2F%20WPCS%3A%20unprepared%20SQL%20ok.%20%0A%0A%09return%20%24object_ids%3B%20%0A%7D%0A%0A%7D” message=”” highlight=”” provider=”manual”/]