• Resolved jasghar

    (@jasghar)


    Hi Rank Math team,

    When saving a post, I would like to automatically set the post to “noindex.”

    I tried updating the post meta key rank_math_robots with the value “noindex,” and while the meta key value does get updated in the database, it doesn’t reflect on the frontend. The robots tag on the frontend remains as “index.”

    Additionally, in the Rank Math sidebar of the post edit screen, the post is still marked as “index,” and the checkboxes for “noindex,” “nofollow,” etc., are not selected.

    Could you please guide me on how to properly set a post to “noindex” when publishing or updating it?

    Here’s the code I’m currently using.

    function ayl_noindex_msn_post( $post_id ) {
    if( has_term( 'slideshow', 'feed_type', $post_id ) ) {
    $arr = ['noindex', 'nofollow', 'noarchive', 'noimageindex', 'nosnippet'];
    update_post_meta( $post_id, 'rank_math_robots', serialize( $arr ) );
    }
    }

    add_action( 'save_post', 'ayl_noindex_msn_post' );

    Could you please let me know what might be missing or how to correctly set a post to “noindex” when publishing or updating it?

    Thanks!

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @jasghar,
    ?
    Thank you for contacting Rank Math support.
    ?
    Instead of modifying the robots meta tag in the database, you can set the default robots meta of your posts to noindex and nofollow in Rank Math.
    ?
    Head over to Rank Math > Titles & Meta > Post > Post Robots Meta and set the robots meta tag there.
    ?
    Once you publish your posts, the default robots meta tag will be applied.
    ?
    Looking forward to helping you.

    Thread Starter jasghar

    (@jasghar)

    Hi @rankmathsupport , thanks for the reply

    I do know about the setting you mentioned but it will not work in my case because it will set meta tag on each and every post

    but the thing is I have a custom taxonomy ‘feed_type’ in my website and this custom taxonomy have a term ‘slideshow’ I only want to noindex the posts where the taxonomy is feed_type and term is slideshow, that’s why I was using this code

    function ayl_noindex_msn_post( $post_id ) {
    if( has_term( 'slideshow', 'feed_type', $post_id ) ) {
    $arr = ['noindex', 'nofollow', 'noarchive', 'noimageindex', 'nosnippet'];
    update_post_meta( $post_id, 'rank_math_robots', serialize( $arr ) );
    }
    }

    add_action( 'save_post', 'ayl_noindex_msn_post' );

    to noindex the post on save only if the post have taxonomy of feed_type and term of slideshow

    Hope it make more sense to you

    Please let me know how can noindex the posts specifically of taxonomy feed_type

    Thanks

    • This reply was modified 2 months, 1 week ago by jasghar. Reason: accidentally saved the post without completing it
    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @jasghar,

    In this case, you can also make use of our robot meta filter code to modify the robot meta right before the page loads.

    Here’s a code you may try:

    add_filter('rank_math/frontend/robots', function ($robots) {global $post;if( has_term( 'slideshow', 'feed_type', $post->ID ) ) {$robots['index'] = 'noindex';$robots['follow'] = 'nofollow';$robots['archive'] = 'noarchive';$robots['imageindex'] = 'noimageindex';$robots['snippet'] = 'nosnippet';}return $robots;});

    Let us know if this works for you.

    Looking forward to helping you.

    Thread Starter jasghar

    (@jasghar)

    Hi @rankmathsupport

    Thank you for the response and the code you provided is working to some extent but I guess this is not what I want

    See it’s only setting the post to noindex on frontend but if I open the backend of the post and go to advance tab I see the ‘index’ checkbox is checked

    IDK if I should be concerned about this or not, but it will be hard for me to check from the back if the post if marked as noindex or not

    Also, I’m bit concerned about the search engine crawlers, like if I use the provided code will they skip indexing the slideshow posts or will they index these posts? because I dont want search engines to crawl these posts with term slideshow


    Thanks

    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @jasghar,

    In that case, to make the code work, you’ll have to change it a bit. You can try removing the serialize() function to make it work. Here’s the code:

    function update_rank_math_robots_meta( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
    }

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
    return;
    }

    if( has_term( 'slideshow', 'feed_type', $post_id ) ) {
    $arr = array(
    "noindex",
    "noarchive",
    "nosnippet",
    "nofollow",
    "noimageindex"
    );
    update_post_meta( $post_id, 'rank_math_robots', $arr );
    }
    }
    add_action( 'save_post', 'update_rank_math_robots_meta' );

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Thread Starter jasghar

    (@jasghar)

    @rankmathsupport Thanks much, this solution is working, before reaching out to you I was doing the same previously but the only mistake I was making was saving the array in serialized format.

    update_post_meta( $post_id, 'rank_math_robots',  serialize($arr)  );

    Thank you for the help

    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @jasghar,
    ?
    Glad that helped.
    ?
    Don’t hesitate to get in touch with us if you have any other questions.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.