Exclude draft or private status post

If you would like to exclude page who is in draft or private you can add the code below:

<?php

add_action('pre_get_posts', 'custom_weglot_translated_search_redirect');
function custom_weglot_translated_search_redirect() {
    global $post;
    if ($post) {
        $post_status = get_post_status($post->ID);
        if (!function_exists('weglot_get_original_language') || !function_exists('weglot_get_current_language')) {
            return;
        }
        if (($post_status == 'draft' || $post_status == 'private') && !is_admin() && weglot_get_original_language() != weglot_get_current_language()) {
            wp_redirect(weglot_get_full_url_no_language(), 301);
            exit;
        }
    }
}

add_filter('weglot_button_html', 'custom_weglot_translated_search_no_selector');
function custom_weglot_translated_search_no_selector($button_html) {
    global $post;
    if ($post) {
        $post_status = get_post_status($post->ID);
        if (($post_status == 'draft' || $post_status == 'private') && !is_admin()) {
            return '';
        }
    }
    return $button_html;
}

Last updated