Skip to content

Instantly share code, notes, and snippets.

@djrmom
djrmom / custom-hooks.php
Created October 5, 2017 17:30
facetwp different posts per page on initial load
<?php
/**
** requires at least 3.0.4
**/
add_filter( 'facetwp_query_args', function( $query_args, $class ) {
if ( 'posts_template' == $class->ajax_params['template'] && empty( $class->facets ) && $class->is_preload ) {
$query_args['posts_per_page'] = 1;
}
return $query_args;
}, 10, 2 );
@djrmom
djrmom / custom-hooks.php
Last active April 19, 2018 15:27
facetwp preload variables
<?php
/** preload facet for specific page **/
add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
if ( 'my_page' == FWP()->helper->get_uri() ) {
if ( empty( $url_vars ) ) { // or check a specific facet empty( $url_vars['some_facet'] )
$value = 'something' // 'something' should be looked up for 'my_page'
$url_vars['some_facet'] = array( $value );
}
}
return $url_vars;
@djrmom
djrmom / custom-hooks.php
Last active February 16, 2018 21:01
facetwp force query args
<?php
/** to use this make sure all queries to be used
** for facets have 'facetwp' => true in query args, including the
** query args setting in a facetwp template
**/
// add 'facetwp' => false anytime it is not already set
add_action( 'pre_get_posts', function( $query ) {
if ( ! isset( $query->query_var['facetwp'] ) ) {
$query->set( 'facetwp', false );
@djrmom
djrmom / settings.php
Created October 13, 2017 19:57
facetwp map marker
<div id="fwpm-infobox">
<h1 class="fwpm-infobox-title"><?php the_title(); ?></h1>
<div class="fwpm-infobox-content"><?php the_excerpt(); ?></div>
</div>
@djrmom
djrmom / facet.js
Last active October 17, 2017 15:35
facetwp reset another facet on dropdown change
(function($) {
$(document).on('change', '.facetwp-facet-product_categories', function() { // change product_categories to name of dropdown facet
FWP.is_reset = true;
FWP.facets['product_tags'] = []; // set other facet to no selections
delete FWP.facets['paged']; // remove "paged" from URL
FWP.refresh();
});
$(document).on('change', '.facetwp-facet-product_tags', function() { // change product_tags to name of dropdown facet
FWP.is_reset = true;
FWP.facets['product_categories'] = []; // set other facet to no selections
@djrmom
djrmom / custom-hooks.php
Last active April 19, 2018 15:29
facetwp preselect
<?php
add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
if ( 'demo/cars' == FWP()->helper->get_uri() ) {
if ( empty( $url_vars['make'] ) && empty( $url_vars['other_facet'] ) ) {
$url_vars['make'] = array( 'audi' );
$url_vars['other_facet'] = array( 'something' );
}
} elseif ( 'demo/otherpage' == FWP()->helper->get_uri() ) {
if ( empty( $url_vars['make'] ) && empty( $url_vars['other_facet'] ) ) {
@djrmom
djrmom / custom-hooks.php
Last active November 21, 2017 18:23
facetwp change author display
<?php
// re-index your facets after adding this code
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'author' == $params['facet_name'] ) {
$user = get_user_by( 'id', $params['facet_value'] );
if ( false !== $user ) {
$params['facet_display_value'] = $user->last_name . ', ' . $user->first_name
}
}
return $params;
@djrmom
djrmom / custom-hooks.php
Created October 27, 2017 13:28
facetwp date source converted to year
<?php
/**
* reindex after adding or updating this filter
*/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'date_as_year' == $params['facet_name'] ) { // change date_as_year to name of your facet
$raw_value = $params['facet_value'];
$params['facet_value'] = date( 'Y', strtotime( $raw_value ) );
$params['facet_display_value'] = $params['facet_value'];
$(document).on('facetwp-loaded', function() {
$('.facetwp-facet').each(function() {
var facet_name = $(this).attr('data-name');
var facet_label = FWP.settings.labels[facet_name];
if ( 'undefined' !== typeof FWP.settings.num_choices[facet_name] && FWP.settings.num_choices[facet_name] > 0 && $('.facet-label[data-for="' + facet_name + '"]').length < 1 ) {
$(this).before('<h3 class="facet-label" data-for="' + facet_name + '">' + facet_label + '</h3>');
}
});
});
@djrmom
djrmom / custom-hooks.php
Created November 16, 2017 16:01
facetwp select all
<?php
/**
* attaches a click even to a div that selects all of a checkbox facets available options and triggers a refresh
* change "product_categories" to the name of your facet
* <div class="facetwp-checkbox select-all">Select all</div> creates a div that looks like facet checkboxes
*
*/
add_action( 'wp_head', function() { ?>
<script>