Created
May 11, 2021 09:00
-
-
Save mohammadYousefiDev/4010190fb8a5d7f5a7c3d89b5fec9cdc to your computer and use it in GitHub Desktop.
Products have thumbnail filter in product list in woocommece admin product list
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
function my_custom_product_filters( $post_type ) { | |
$value1 = ''; | |
$value2 = ''; | |
if( isset( $_GET['p_thumb'] ) ) { | |
switch( $_GET['p_thumb'] ) { | |
case 0: | |
$value1 = ' selected'; | |
break; | |
case 1: | |
$value2 = ' selected'; | |
break; | |
} | |
} | |
if( $post_type == 'product' ) { | |
echo '<select name="p_thumb">'; | |
echo '<option value>فیلتر بر اساس تصویر شاخص</option>'; | |
echo '<option value="0" '.$value1.'>بدون تصویر شاخص</option>'; | |
echo '<option value="1" '.$value2.'>همراه با تصویر شاخص</option>'; | |
echo '</select>'; | |
} | |
} | |
add_action( 'restrict_manage_posts', 'my_custom_product_filters' ); | |
function apply_my_custom_product_filters( $query ) { | |
global $pagenow; | |
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['p_thumb'] ) && $_GET['p_thumb'] != '' && $_GET['post_type'] == 'product' ) { | |
if( $_GET['p_thumb'] == 1 ) { | |
$query->set( 'meta_query', array(array('key' => '_thumbnail_id')) ); | |
} | |
else { | |
$query->set( 'meta_query', array( | |
array( | |
'key' => '_thumbnail_id', | |
'value' => '?', | |
'compare' => 'NOT EXISTS' | |
) | |
) ); | |
} | |
} | |
} | |
add_action( 'pre_get_posts', 'apply_my_custom_product_filters' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment