Gists

by mgibbs189 - 5 years ago

<?php

function fwp_get_post_coords( output, $class ) {
    $coords = array();
    $post_ids = (array) $class->query_args['post__in'];

    foreach ( $post_ids as $post_id ) {
        $coords[ $post_id ] = array(
            'title' => get_the_title( $post_id ),
            'lat' => GET_THE_LATITUDE( $post_id ),
            'lng' => GET_THE_LONGITUDE( $post_id )
        );
    }

    $output['settings']['coords'] = $coords;

    return $output;
}

add_filter( 'facetwp_filtered_post_ids', 'fwp_get_post_coords', 10, 2 );
by mgibbs189 - 3 months ago

<?php

// add to your (child) theme's functions.php
// this unregisters the proximity facet's "post ID re-sorting" hook

add_filter( 'facetwp_query_args', function( $query_args ) {
    remove_filter( 'facetwp_filtered_post_ids', [ FWP()->helper->facet_types['proximity'], 'sort_by_distance' ] );
    return $query_args;
} );

by mgibbs189 - 4 months ago

<?php

/**
 * Grab the premium posts and _intersect_ them at the beginning of the final ID array
 */
add_filter( 'facetwp_filtered_post_ids', function( $post_ids, $class ) {
    $premium_query_args = array(
        'fields' => 'ids',
        'facetwp' => false,
        'post_type' => array('photographer', 'videographer'),
        'post_status' => 'publish',
        'orderby' => 'rand',
        'posts_per_page' => -1,
        'tax_query' => array(
        'relation' => 'OR',
            array(
                'taxonomy' => 'photographer-category',
                'fields' => 'slug',
                'terms' => 'premium-member-photographer',
            ),
            array(
                'taxonomy' => 'videographers_category',
                'fields' => 'slug',
                'terms' => 'premium-member-videographer',
            ),
        )
    );

    $premium_query = new WP_Query( $premium_query_args );
    $premium_ids = $premium_query->posts;

    // narrow premium IDs to those contained within $post_ids
    $premium_ids = array_values( array_intersect( $premium_ids, $post_ids ) );

    if ( ! empty( $premium_ids ) ) {
        $temp_post_ids = $post_ids;
        $post_ids = $premium_ids;
        foreach ( $temp_post_ids as $id ) {
            $post_ids[] = $id;
        }
    }

    return $post_ids;
}, 10, 2 );

by mgibbs189 - 12 months ago

<?php

add_filter( 'facetwp_filtered_post_ids', function( $post_ids, $class ) {
    if ( ! empty( FWP()->or_values ) ) {
        $post_ids = [];
        foreach ( FWP()->or_values as $facet_name => $matches ) {
            foreach ( $matches as $match ) {
                $post_ids[] = $match;
            }
        }
        $post_ids = array_unique( $post_ids );
    }
    return $post_ids;
}, 10, 2 );
by mgibbs189 - 3 years ago

<?php

// Add the following to your (child) theme's functions.php
add_filter( 'facetwp_filtered_post_ids', function( $post_ids, $class ) {
    global $wpdb;

    // Lookup featured post IDs
    $sql = "
    SELECT post_id
    FROM {$wpdb->prefix}postmeta
    WHERE meta_key = '_featured' AND meta_value = '1' AND post_id IN (" . implode( ',', $post_ids ) . ")";
    $results = $wpdb->get_col( $sql );

    // If a featured post, remove it from $post_ids and add it to $matches
    // We loop through $post_ids to preserve featured order
    $matches = array();
    foreach ( $post_ids as $key => $post_id ) {
        if ( in_array( $post_id, $results ) ) {
            $matches[] = $post_id;
            unset( $post_ids[ $key ] );
        }
    }

    // Featured first, then default sort
    $post_ids = array_merge( $matches, $post_ids );

    return $post_ids;
}, 15, 2 );

by mgibbs189 - 3 years ago

<?php

add_filter( 'facetwp_filtered_post_ids', function( $post_ids, $class ) {
    $params = $class->ajax_params;
    $sort_key = empty( $params['extras']['sort'] ) ? 'default' : $params['extras']['sort'];

    if ( 'distance_desc' == $sort_key ) {
        // @TODO reverse $post_ids
    }
    return $post_ids;
}, 12, 2 );

by mgibbs189 - 3 years ago

<?php

add_filter( 'facetwp_filtered_post_ids' function( $post_ids, $class ) {
    $class->is_search = false; // prevent changing orderby
    return $post_ids;
}, 10, 2 );

by mgibbs189 - 4 years ago

<?php

add_filter( 'facetwp_pre_filtered_post_ids', 'store_pre_filtered_post_ids' );
add_filter( 'facetwp_filtered_post_ids', 'store_filtered_post_ids' );

function store_pre_filtered_post_ids( $post_ids ) {
    $GLOBALS['unfiltered_ids'] = $post_ids;
    return $post_ids;
}

function store_filtered_post_ids( $post_ids ) {
    $GLOBALS['filtered_ids'] = $post_ids;

    if ( isset( $GLOBALS['unfiltered_ids'] ) ) {
        return $GLOBALS['unfiltered_ids'];
    }
    return $post_ids;
}


<?php $filtered_ids = isset( $GLOBALS['filtered_ids'] ) ? $GLOBALS['filtered_ids'] : array(); ?>

<?php while ( have_posts() ): the_post(); ?>
    <?php $classname = in_array( get_the_ID(), $filtered_ids ) ? '' : 'ghost'; ?>
    <p class="<?php echo $classname; ?>">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </p>
<?php endwhile; ?>