• Is there any way of adding an additional post status. I have a number of posts I’d like to archive but not delete. Is there any way to add a function to enable this? I’ve tried a few plugins that claim to do this but none of them seem to work.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Some custom coding could accomplish this, but it gets pretty messy. Why don’t you just switch the post back to draft or make it private? You keep the post but it will not be shown on the front end. Works with no added code or plugins.

    Thread Starter greencode

    (@greencode)

    Sorry I didn’t reply sooner. Private posts still show on the site though – I realise the user won’t be able to read the post but I don’t want it on the site at all. Drafts, currently, are probably the only solution – not ideal as if we need to use that for actual Drafts then it could get messy. Find it very strange this isn’t an easy thing in WordPress.

    Moderator bcworkz

    (@bcworkz)

    What would be a fair bit easier than status is to pick a particular category term (or custom taxonomy term if you prefer) that indicates archive status. Then omit all such posts from all front end queries through the “pre_get_posts” action. There is an example near the bottom of the linked article you could adapt and place in functions.php of your theme.

    Adapt with $query->set( 'category__not_in', ARCHIVE_ID );
    Either define ARCHIVE_ID or replace with the actual value.

    FWIW, these forums themselves extensively use an archive “status” which is in fact a taxonomy term.

    Thread Starter greencode

    (@greencode)

    That’s great. Thanks for this. I’ve managed to add that and now no posts are showing in the archive page but they are still showing in all other post queries. How would I also block the individual posts from being picked up Google – just use robots to discourage Google?

    Thread Starter greencode

    (@greencode)

    Sorry, this is what I’m using:

    
    function target_main_category_query_with_conditional_tags( $query ) {
     
        if ( ! is_admin() && $query->is_main_query() ) {
            if ( is_category() ) {
                $query->set( 'category__not_in', 1147 );
            }
        }
    }
    
    Thread Starter greencode

    (@greencode)

    Ah, looks like this works:

    
    add_action( 'pre_get_posts', 'target_main_category_query_with_conditional_tags' );
     
    function target_main_category_query_with_conditional_tags( $query ) {
     
        if ( ! $query->is_main_query() ) {
                $query->set( 'category__not_in', 1147 );
        }
    }
    

    Still just need to know about stopping posts from showing with a direct URL to the post and stop indexing them in Search Engines.

    Moderator bcworkz

    (@bcworkz)

    You need a noindex meta tag in the head section for all such posts:
    <meta name="robots" content="noindex">
    As the head section template (header.php) is common to all posts, you need to conditionally output the tag only when is_single() is true and the requested post has the 1147 category ID assigned to it.

    The tag could be injected through the “wp_head” action, but IMO the tag is better off as one of the first tags encountered. The only way to do that is to add the conditional PHP directly to the header.php template. A third option is to have the server send a X-Robots-Tag: noindex header response.

    Oddly, though the meta tag output is necessarily outside of the “Loop”, the loop’s global $post object seems to already have the requested post object assigned, which can be passed directly to other post related functions. A more logical way to get the needed post ID is from get_queried_object_id(). Use get_the_terms() to check for the 1147 category because it will use a cached value if it can. wp_get_post_terms() will always hit the DB. It’s important for this code to be efficient because it runs for every front end request.

    To send a header instead, use the ‘wp_headers’ filter, conditionally adding the noindex header to the passed array of header data.

    Be aware that obeying the noindex tag is optional. Not all search bots will honor it. The major search engine bots like Google say they honor it. Also, Google for one will still crawl the page and follow any links it finds. It honors the noindex tag only by not listing the associated page in search results.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add new post status’ is closed to new replies.