Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active February 28, 2018 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/4606ee59a04771b54ea36f73ae55fd41 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/4606ee59a04771b54ea36f73ae55fd41 to your computer and use it in GitHub Desktop.
[MarketPress - Upfront] - Products search
<?php
/**
* Plugin Name: [MarketPress - Upfront] - Products search
* Plugin URI: https://premium.wpmudev.org/
* Description: MarketPress Products search for Upfront
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'upfront-posts-get_markup-before', function( $status ){
$queried_type = $query->get( 'post_type' );
$is_product_queried = is_array( $queried_type ) ? in_array( MP_Product::get_post_type(), $queried_type ) : MP_Product::get_post_type() == $queried_type;
if (is_search() && !is_admin() && $is_product_queried ) {
$query->set( 's', get_query_var('s') );
}
return $query;
}, 0 );
function wpmudev_mp_uf_posts_where( $where ){
remove_action( 'posts_where', 'wpmudev_mp_uf_posts_where', 10 );
global $wpdb;
$s = sanitize_text_field( $_GET['s'] );
$post_type = MP_Product::get_post_type();
$where = " AND {$wpdb->posts}.post_type = '{$post_type}' AND {$wpdb->posts}.post_status = 'publish' AND ( {$wpdb->posts}.post_title LIKE '%{$s}%' OR {$wpdb->posts}.post_content LIKE '%{$s}%' OR {$wpdb->posts}.post_excerpt LIKE '%{$s}%' )";
return $where;
}
function wpmudev_look_only_products($query) {
if (is_search() && !is_admin() && MP_Product::get_post_type() == $query->get( 'post_type' ) ) {
$query->set( 's', get_query_var('s') );
}
return $query;
}
add_filter('pre_get_posts', 'wpmudev_look_only_products');
@Spoygg
Copy link

Spoygg commented Feb 7, 2018

Nice 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment