2
votes

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?

1

1 Answers

0
votes

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');