2017-11-22 3 views
0

woocommerce 제품 태그 쿼리 내에 표시되는 "예비 부품"범주의 제품을 어떻게 제외시킬 수 있습니까? 나는 모든 제품을/tags/안에 목록으로 만들고 싶지만 그 제품이 "예비 부품"범주 안에 있다면 그 안에는 글꼴 목록 만 있습니다. 어떤 기능을 사용해야합니까?제품 태그 내의 특정 제품 카테고리에서 Woocommerce 제품을 제외하십시오.

나는 웹을 검색했지만 그 일은하지 않았다.

+0

이 문제를 해결하기 위해 노력했다 뭐? MySQL을 사용할 계획이라면이 테이블은 어떻게 구성되어 있습니까? https://stackoverflow.com/help/how-to-ask –

답변

1

woocommerce_product_query_tax_query 필터 후크에 연결되는이 사용자 지정 기능에서는이 작업을 쉽게 수행 할 수 있습니다.

그래서 "spare-parts" 제품 카테고리 슬러그의 모든 제품은 제품 태그 아카이브 페이지에서 제외됩니다. 어떤 플러그인 파일도

add_filter('woocommerce_product_query_tax_query', 'exclude_specific_product_category_query', 10, 2); 
function exclude_specific_product_category_query($tax_query, $query) { 
    // Only on Product Tag archives pages 
    if(is_admin() || ! is_product_tag()) return $tax_query; 

    // HERE Define your product category SLUGs to be excluded 
    $terms = array('spare-parts'); // SLUGs only 

    // The taxonomy for Product Categories 
    $taxonomy = 'product_cat'; 

    // Add your criteria 
    $tax_query[] = array(
     'taxonomy' => $taxonomy, 
     'field' => 'slug', // Or 'name' or 'term_id' 
     'terms' => $terms, 
     'operator' => 'NOT IN', // Excluded 
    ); 

    return $tax_query; 
} 

코드 활성 자식 테마 (또는 테마)의 function.php 파일에 간다 나 : 여기

는 코드입니다.

테스트를 거쳐 작동합니다.


그래서 당신이 당신이 몇 가지 제품 범주를 제외하고 일부 제품의 태그를 포함합니다 어디 유사한 tax_query를 사용하는 WP_query에 사용해야하는 경우 ...

는 것이 예에서 제품 ID가 혼동으로 구분 된 문자열을 출력합니다.

// HERE Define your product category SLUGs to be excluded 
$terms_cat = array('spare-parts'); // SLUGs only 

// HERE Define your product tag SLUGs to be included 
$terms_tag = array('special', 'wear'); // SLUGs only 

// The taxonomies 
$taxonomy_tag = 'product_tag'; // Product tag 
$taxonomy_cat = 'product_cat'; // Product Category 

// The Query 
$loop = new WP_Query(array(
    'post_type' => 'product', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'order' => 'ASC', 
    'tax_query' => array(
     'relation' => 'AND', 
     array(// The tag query (Include) 
      'taxonomy' => $taxonomy_tag, 
      'field' => 'slug', 
      'terms' => $terms_tag, 
     ), 
     array(// the category query (exclude) 
      'taxonomy' => $taxonomy_cat, 
      'field' => 'slug', // Or 'name' or 'term_id' 
      'terms' => $terms_cat, 
      'operator' => 'NOT IN', // Excluded 
     ), 
    ) 
)); 

if ($loop->have_posts()): 
    while ($loop->have_posts()): 
     $loop->the_post(); 
     $products_ids[] = $loop->post->ID; // Set the product Ids in an array 
    endwhile; 
endif; 

// always reset the WP_Query 
wp_reset_query(); 

// Convert the array in a string with all product Ids coma separated 
$product_ids_string = implode(', ', $products_ids); 

// Output Ids 
echo $product_ids_string; 

테스트를 거쳐 작동합니다.


관련 문서 : WP_Query - Taxonomy Parameters

+1

빠른 답변을 주셔서 감사합니다. "endif;"오류가 발생합니다. code line –

+0

@DigitalWhirlLLC 나는이 업데이트를 끝내고 큰 업데이트를하고이 작은 실수를 바로 잡았다. 이제는 너의 모든 것을 만들 수있다. – LoicTheAztec

관련 문제