/ facetten
Dit filter wordt aangeroepen bij het ophalen van facetten in de admin-interface en op de frontend om facetten en query-inhoud weer te geven.
Het laat toe om uw eigen facetten toe te voegen met uw eigen logica. Het maken van aangepaste facetten vereist geavanceerde kennis van PHP- en SQL-talen
Argument | Type | Omschrijving |
$facets | array | Bevat geregistreerde facetargumenten |
[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”/]
De plug-in zal de volgende methoden gebruiken, indien beschikbaar, vanuit uw facet klasse
query_facet
: Om facetwaarden / namen op te vragen, afhankelijk van de inhoud en filterstatus.render_facet
: Om facetwaarden / namen weer te geven die worden opgevraagd doorquery_facet
methode.query_objects
: Om inhoud te filteren door object-id's (post-id's, term-id's of gebruikers-id's) uit geselecteerde facetwaarden te zoeken.
Met deze facetklasse kan bijvoorbeeld een alfabetische lijst worden gemaakt om inhoud op de eerste letter te filteren:
[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”/]