简体   繁体   中英

Auto noindex for out of stock products in woocommerce wordpress

I am using WooCommerce in WordPress and want to automatically add noindex for out of stock products. Currently I can put noindex for sold category by using this code

function add_tagseo_metarob() {
    if ('product' == get_post_type()){
        if ( has_term( array('SOLD'), 'product_cat' )) {
        ?>
        <meta name="robots" content="noindex">

        <?php
        }
    }

}

add_action('wp_head', 'add_tagseo_metarob');

this code works but I have to manually update the individual product into sold category.

Is there any similar solution that I can automatically add noindex for out of stock products?

Try this

function add_tagseo_metarob() {
    if ( get_post_type( get_the_ID() ) == 'product'){
        $pro = new WC_Product(get_the_ID());
        if( $pro->stock_status != 'instock' ){
            ?>
             <meta name="robots" content="noindex">

            <?php
        }
    }
}

add_action('wp_head', 'add_tagseo_metarob');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM