Skip to content

Instantly share code, notes, and snippets.

@MjHead
Created September 26, 2019 10:35
Show Gist options
  • Save MjHead/0f4bb7d67b5c90abc42c914e64758db1 to your computer and use it in GitHub Desktop.
Save MjHead/0f4bb7d67b5c90abc42c914e64758db1 to your computer and use it in GitHub Desktop.
JetSmartFilters. Filter JetEngine listing grid by author and post type.
<?php
/**
* Register new query variables for post type and author
*
* @param array $vars Default query variables array
* @return array
*/
function my_register_filter_query_vars( $vars ) {
array_unshift( $vars, 'post_type', 'author' );
return $vars;
}
add_filter( 'jet-smart-filters/query/vars', 'my_register_filter_query_vars' );
/**
* Add required variables from request to filtered query arguments.
* You can use this if you need to make some actions with $value before sending it into query
*
* @param string $value Raw value from request.
* @param string $key Raw key from request.
* @param string $var Currently processed query variable name.
* @param object $query Jet_Smart_Filters_Query_Manager instance.
* @return string
*/
function my_process_filter_query_vars( $value, $key, $var, $query ) {
return $value;
}
add_filter( 'jet-smart-filters/query/add-var', 'my_process_filter_query_vars', 10, 4 );
/**
* Remove aproppriate rows from meta query (because plugin is always added all unknown data as meta query)
*
* @param array $row Meta query row.
* @return array
*/
function my_clear_meta_query( $row ) {
if ( in_array( $row['key'], array( 'author', 'post_type' ) ) ) {
$row = array();
}
return $row;
}
add_filter( 'jet-smart-filters/query/meta-query-row', 'my_clear_meta_query' );
/**
* Add required post types to initial query arguments
* 188 - is listing ID which should be filterable. You need to replace it with your own.
*
* @param array $args Query arguments array
* @param object $widget Widget instance
* @return array
*/
function my_add_initial_post_types_to_grid( $args, $widget ) {
$settings = $widget->get_widget_settings();
if ( empty( $settings['lisitng_id'] ) || 188 !== absint( $settings['lisitng_id'] ) ) {
return $args;
}
$post_types = array_unique( array_merge(
array( $args['post_type'] ),
array( 'post', 'page', 'events', 'apartments' )
) );
$args['post_type'] = $post_types;
return $args;
}
add_filter( 'jet-engine/listing/grid/posts-query-args', 'my_add_initial_post_types_to_grid', -1, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment