Skip to content

Instantly share code, notes, and snippets.

@Archie22is
Created April 12, 2023 12:00
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 Archie22is/e18162253af91e19e6a4a2072ba7f57e to your computer and use it in GitHub Desktop.
Save Archie22is/e18162253af91e19e6a4a2072ba7f57e to your computer and use it in GitHub Desktop.
Unlist, index and redirect - WooCommerce Shit
<?php
/**
* Hide specific prohects
* @author Archie M
*
*/
function piglet_remove_posts_from_home_page( $query ) {
if( $query->is_main_query() && $query->is_home() ) {
$query->set( 'post__not_in', array( 1027335, 1027302, 1027259 ) );
}
}
add_action( 'pre_get_posts', 'piglet_remove_posts_from_home_page' );
/**
*
*
*/
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'groupon' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
/**
* Hide 'Groupon' anywhere on site but in admin
* @author Archie M
*
*/
function hide_category( $terms ) {
$new_terms = array();
foreach ( $terms as $term ) {
if ( $term->slug !== 'groupon' ) {
$new_terms[] = $term;
} else if ( $term->taxonomy !== 'product_cat' || is_admin() ) {
$new_terms[] = $term;
}
}
return $new_terms;
}
add_filter( 'get_terms', 'hide_category', 10, 1 );
/**
* Redirect 'Groupon' product category to shop page
* @author Archie M
*
*/
function groupon_template_redirect() {
if ( is_product_category( 'groupon' ) ) {
//if ( is_product( '/product-category/groupon/' ) ) {
//$url = site_url( '/product-category/groupon' );
//wp_safe_redirect( $url, 301 );
wp_safe_redirect( home_url() . '/piglets-pantry-is-an-award-winning-food-producer-based-in-sussex/' );
exit();
}
}
add_action( 'template_redirect', 'groupon_template_redirect' );
/**
* Do not index listed products
* @author Archie M
*
*/
function product_noindex_meta_tags() {
global $product;
$no_robots_products = array(1027335,1027302,1027259); // list of product ids
if ( $product && in_array($product->get_id(), $no_robots_products) ) {
echo '<meta name="robots" content="noindex" />';
}
}
add_action('wp_head', 'product_noindex_meta_tags');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment