Skip to content

Instantly share code, notes, and snippets.

@djrmom
djrmom / display.php
Last active April 7, 2018 03:53
facetwp basic 3 column responsive grid
<div class="fwp-grid">
<?php while ( have_posts() ) : the_post(); ?>
<div class="fwp-grid-col">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
<?php endif; ?>
<?php the_title( '<h3><a href="' . esc_url( get_the_permalink() ) . '">', '</a></h3>' ); ?>
</div>
<?php endwhile; ?>
</div>
<?php
/**
* adds js code to check for any facets being selected
*/
add_action( 'wp_head', function() { ?>
<script>
(function($) {
$(document).on('facetwp-loaded', function() {
var facets_in_use = function() {
var in_use = false;
@djrmom
djrmom / custom-hooks.php
Last active July 19, 2018 16:55
facetwp add tags to category facet
<?php
add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
if ( 'cases_topics' == $params['facet']['name'] ) {
$post_id = $params['defaults']['post_id'];
$tags = get_tags( $post_id );
if ( !empty( $tags ) ) {
foreach ( $tags as $tag ) {
$new_row = $params['defaults'];
$new_row['facet_source'] = 'tax/post_tag';
@djrmom
djrmom / custom-hooks.php
Last active August 24, 2020 17:45
facetwp convert prices to price ranges
<?php
add_filter( 'facetwp_index_row', function ( $params, $class ) {
if ( 'my_facet' == $params['facet_name'] ) { // change 'my_facet' to facet name
if ( $params['facet_value'] > 10000 ) {
$new_params = $params;
$new_params['facet_value'] = 50000;
$new_params['facet_display_value'] = 'Less than 50000';
$class->insert( $new_params ); // insert each value to the database
@djrmom
djrmom / custom-hooks.php
Last active August 24, 2020 17:45
facetwp index relationship
<?php
/** make the datasource something you know will always have a value for every post - like post title - it
** is only a placeholder and won't be indexed
**/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'search_new' == $params['facet_name'] ) { // change my_facet to name of your facet
$project_id = $params['post_id'];
@djrmom
djrmom / custom-hooks.php
Created April 24, 2018 17:05
facetwp filter wp search form
<?php
/** filters search form to replace the s input with your search facet - change fwp_search as needed
** and replace the default action with the page url of your custom search results page - change /search-results/ as needed
** may need adjusting if the search is already modified by another plugin or theme */
add_filter( 'get_search_form', function( $form ) {
$form = str_replace( 'name="s"', 'name="fwp_search"', $form );
$form = preg_replace( '/action=".*"/', 'action="/search-results/"', $form );
return $form;
@djrmom
djrmom / custom-hooks.php
Last active April 25, 2018 18:25
facetwp search attachments
<?php
/**
* make sure both publish and inherit (for attachments) are searched on the search page
* Relevanssi has an option to index attachments but the query facet detects does not include attachments
* because it does not include post_status inherit
*/
add_action( 'pre_get_posts', function( $query ) {
if ( $query->is_search() ) {
$statuses = $query->get( 'post_status' );
$query->set( 'post_status', array_merge( (array)$statuses, array( 'publish', 'inherit' ) ) );
@djrmom
djrmom / custom-hooks.php
Created April 25, 2018 21:24
facetwp re-add woocommerce post clauses for sort by price
<?php
/**
* re-add post-clause for woocommerce after facetwp updates the query vars
*/
add_action( 'pre_get_posts', function( $query ) {
if ( 'product_query' == $query->get('wc_query') && true == $query->get('facetwp' ) && 'price' == $query->get('orderby') ) {
if ( 'DESC' == $query->get('order') ) {
add_filter( 'posts_clauses', array( wc()->query, 'order_by_price_desc_post_clauses' ) );
} else {
@djrmom
djrmom / custom-hooks.php
Created May 1, 2018 16:43
facetwp conditional scroll to top
<?php
/** adds script to header with a conditional check to not scroll to top
** when the load more button has been clicked
**/
add_action( 'wp_head', function() { ?>
<script>
(function($) {
$(document).on('facetwp-refresh', function() {
if ( FWP.is_load_more == true ) {
@djrmom
djrmom / custom-hooks.php
Created May 2, 2018 17:01
facetwp scroll to top on wp_head hook
<?php
/** adds script to header with a conditional check to not scroll to top
** when the load more button has been clicked
**/
add_action( 'wp_head', function() { ?>
<script>
(function($) {
$(document).on('facetwp-loaded', function() {
if (FWP.loaded) {