Last active
May 31, 2019 06:55
-
-
Save wpmudev-sls/1c1594901c4ee19875bc6ed11c6fa679 to your computer and use it in GitHub Desktop.
[MarketPress] - Enabled Search By SKU From Admin Panel Product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [MarketPress] - Enabled Search By SKU From Admin Panel Product | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Author: WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_mp_enabled_search_by_sku_admin_func', 100 ); | |
function wpmudev_mp_enabled_search_by_sku_admin_func() { | |
if ( defined( 'MP_VERSION' ) && class_exists( 'Marketpress' ) ) { | |
// only for admin screen | |
if( ! is_admin() ) return; | |
global $pagenow; | |
if ( $pagenow !== 'edit.php' || ! isset( $_GET[ 'post_type' ] ) || 'product' !== $_GET[ 'post_type' ] || empty( $_GET['s']) ) return; | |
function wpmudev_mp_enabled_search_by_sku_admin_panel_product( $per_page ) { | |
global $wp_query; | |
if( ! $wp_query->is_search || $wp_query->found_posts ){ | |
return $per_page; | |
} | |
unset( $wp_query->query['s'] ); | |
$old_meta_query = isset( $wp_query->query['meta_query'] ) ? $wp_query->query['meta_query'] : false; | |
$wp_query->query['meta_query'] = array( | |
array( | |
'key' => 'sku', | |
'value' => $_REQUEST['s'], | |
'compare' => 'LIKE' // '=' for absolute, 'LIKE' for relactive | |
) | |
); | |
$wp_query = new WP_Query( $wp_query->query ); | |
// fix error in touch_time | |
if( ! isset( $GLOBALS['post'] ) && ! empty( $wp_query->post ) ){ | |
$GLOBALS['post'] = $wp_query->post; | |
} | |
// Set query back | |
if( $old_meta_query ){ | |
$wp_query->query['meta_query'] = $old_meta_query; | |
} | |
$wp_query->set('s', $_REQUEST['s']); | |
return $per_page; | |
} | |
add_action( 'edit_product_per_page', 'wpmudev_mp_enabled_search_by_sku_admin_panel_product' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment