我为Product\\u类型分类法创建了一个称为“stock\\u records”的术语。我希望它是可见的管理只。但它们显示在前端。
最终,其他帖子将引用stock\\u记录来显示信息,但我不希望它们显示在任何前端,不在目录页面中,不在搜索中,不在目录帖子计数中等等。
下面是创建自定义产品类型的工作代码。
/**
* Register the custom product type stock_record after init
*/
function register_stock_record_product_type() {
class WC_Product_Stock_Record extends WC_Product {
public function __construct( $product ) {
$this->product_type = \'stock_record\';
parent::__construct( $product );
}
public function get_type() {
return \'stock_record\';
}
}
}
add_action( \'plugins_loaded\', \'register_stock_record_product_type\' );
/**
* Add to product type drop down.
*/
function add_stock_record_product( $types ){
// Key should be exactly the same as in the class
$types[ \'stock_record\' ] = __( \'Stock Record\' );
return $types;
}
add_filter( \'product_type_selector\', \'add_stock_record_product\' );
/**
* Show pricing fields for stock record product.
*/
function stock_record_custom_js() {
if ( \'product\' != get_post_type() ) :
return;
endif;
?><script type=\'text/javascript\'>
jQuery( document ).ready( function() {
jQuery( \'.options_group.pricing\' ).addClass( \'show_if_stock_record\' ).show();
jQuery(\'.inventory_options\').addClass(\'show_if_stock_record\').show();
jQuery(\'#inventory_product_data ._manage_stock_field\').addClass(\'show_if_stock_record\').show();
jQuery(\'#inventory_product_data ._sold_individually_field\').parent().addClass(\'show_if_stock_record\').show();
jQuery(\'#inventory_product_data ._sold_individually_field\').addClass(\'show_if_stock_record\').show();
});
</script><?php
}
add_action( \'admin_footer\', \'stock_record_custom_js\' );
我可以用以下内容隐藏目录页:
function stock_record_is_visible( $variable , $id) {
$_product = wc_get_product( $id );
if( $_product->is_type( \'stock_record\' ) ) {
return false;
}
return $variable;
}
add_filter( \'woocommerce_product_is_visible\', \'stock_record_is_visible\',10,2 );
但这在搜索中仍然存在。
这是我最近一次未能限制使用pre_get_posts
:
function kisska_query_fix( $query = false ) {
if(!is_admin()) {
$tax_query = $query->get(\'tax_query\', array());
$tax_query[] = array(
\'taxonomy\' => \'product_type\',
\'field\' => \'slug\',
\'terms\' => \'stock_record\',
\'operator\' => \'NOT\'
);
$query->set(\'tax_query\', $tax_query);
}
}
add_action( \'pre_get_posts\', \'kisska_query_fix\' );
如何才能最好地使taxonomy product\\u type term stock\\u记录对用户不可见?:)
最合适的回答,由SO网友:Oleg 整理而成
首先,我在您的“最新失败”代码中看到您使用了错误的“运算符”(或者可能只是输入错误)。如果你把“操作员”改为\'operator\' => \'NOT IN\'
tax query parameters
UPDATE:对于Sablednahcomment
您可以尝试通过修改来完全隐藏此术语(和“计数”)WP_Term_Query 比如:
add_action(\'pre_get_terms\', \'my_pre_get_terms\');
function my_pre_get_terms( $query ) {
if (is_admin())
return;
//if you know term_id you can just set it - "$termIdForExclude = 1234567" and skip next piece of code until "if ( isset( $query->query_vars[\'include\'] ) && !empty( $query->query_vars[\'include\'] ) ) {"
remove_action(\'pre_get_terms\', \'my_pre_get_terms\');//avoid infinite loop
$termForExclude = get_term_by(\'slug\', \'stock_record\', \'product_type\');
add_action(\'pre_get_terms\', \'my_pre_get_terms\');//avoid infinite loop
if ( $termForExclude instanceof WP_Term ) {
$termIdForExclude = $termForExclude->term_id;
} else {
return;
}
//modify query_vars
if ( isset( $query->query_vars[\'include\'] ) && !empty( $query->query_vars[\'include\'] ) ) { //if query has include exclude is ignored
$include = wp_parse_id_list( $query->query_vars[\'include\'] );
$hasId = array_search( $termIdForExclude, $include );
if( $hasId ) {
unset( $include[$hasId] );
}
$query->query_vars[\'include\'] = ( is_array($query->query_vars[\'include\']) ) ? $include : implode(\', \', $include);
if( !empty($include) ) {
return;
}
}
if ( isset( $query->query_vars[\'exclude\'] ) ) {
$exclude = wp_parse_id_list($query->query_vars[\'exclude\']);
if (!in_array($termIdForExclude, $exclude)) {
$exclude[] = $termIdForExclude;
}
$query->query_vars[\'exclude\'] = ( is_array($query->query_vars[\'exclude\']) ) ? $exclude : implode(\', \', $exclude);
} else {
$query->query_vars[\'exclude\'] = $termIdForExclude;
}
}