• Resolved ninsan

    (@jennyrigsjo)


    In the ‘edit.php’ screen in the WP admin section, there is a submenu with different post status options such as All, Mine, Published, Drafts, etc. These menu options are visible only if there are any matching posts, e.g. the ‘Drafts’ option is only visible so long as there is at least one post with the status ‘draft’. But is there a way to always keep these options visible, even if there are no matching posts? For instance, I would like to keep the ‘Trash’ option visible even if there are currently 0 posts with the status ‘trash’.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Jenny, any reason why you want it to be so?

    Moderator bcworkz

    (@bcworkz)

    I think there is a way, but it’s a bit convoluted. Use the “wp_posts_count” filter to save the current counts to a global or static variable, then set all applicable status counts to be at least one (maybe by adding 1 to all of them). This will cause all statuses to be generated, except the counts will now be wrong. Use the “views_edit-post” filter to repair the counts according to the values saved earlier in the other filter callback (or subtract 1 from all of them, so the original values don’t need to be saved).

    That’s the idea anyway, I’ve not verified if this will work. The other possible issue is ensuring that the alteration of counts is only done for the posts list table and not other reasons for getting post counts. Maybe you can check the request in $_SERVER to ensure the posts list table was requested.

    Thread Starter ninsan

    (@jennyrigsjo)

    @wpcoworker
    Short answer: it’s a request from the people for whom I am building a website. ??

    Long answer: I am building a multi-author website in which users can publish and manage their own literary texts (I’m using a custom post type called ‘text’ for this). In order to give the authors some “privacy” I have used the hooks ‘pre_get_posts’ and ‘views_edit-text’ to make it so that authors can only see and count their own texts in the WP admin ‘edit.php’ screen. That way they get to “keep their texts to themselves” until they feel ready to publish/share them. The downside of this setup is that, from an individual author’s point of view, the post status menu options keep appearing and disappearing seemingly at random. For instance, an author may have 0 texts in the trash (or in drafts etc.), yet the Trash option will still be visible in the menu since other authors have texts with the status ‘trash’. Once those authors restore or permanently delete their trashed texts, the Trash options will disappear again, but this sudden disappearance will not make sense from the first author’s point of view.

    Hope this explains my problem a bit better. ??

    @bcworkz
    I am interested in exploring your suggested solution but I am having trouble finding any documentation on the filter ‘wp_posts_count’. Is there anywhere I can read up on its use? ??

    Moderator bcworkz

    (@bcworkz)

    Arg, sorry, it’s “wp_count_posts”. My bad, I sometimes present with dyslexic tendencies. Not to make light of those truly suffering from the disease.
    https://developer.www.ads-software.com/reference/hooks/wp_count_posts/

    That docs page isn’t truly helpful IMO. I usually follow the View on Trac link in the Source section to see its use in context. Scroll up after the jump to see inline docs and how the code sets up the passed parameters.

    views_post-edit is hard to find as well because it’s a dynamic hook. Or maybe not with auto-complete of the search field. Anyway, the docs page:
    https://developer.www.ads-software.com/reference/hooks/views_this-screen-id/

    Thread Starter ninsan

    (@jennyrigsjo)

    So I have managed to come up with a solution to my problem and I thought I’d share it here.

    The basic idea is to manipulate the content of the $views array, which is an associative array containing the links that make up the All, Mine, Published, Drafts (etc.) menu options in the ‘edit.php’ screen. By making sure that all keys in $views are set, it is possible to keep all menu options visible even when one or more of them have no associated posts.

    The below code uses the filter views_edit-{$post-type} to access the $views array. It loops through the array and if it finds a key/post status that isn’t set (due to there being no posts with that particular status), it creates a value/link for that key.

    As you can see I’ve had to create my own translations for the post status menu options (since my website is in a language other than English). Ideally I would like to use WordPress’ built-in functions for translation and localization, but I’m not sure how to go about accessing them from within the filter callback (or if this is even possible or a good idea?).

    Thank you @bcworkz for your suggested filters and links, they helped put me on the right track to solving my issue. ??

    Without further ado, here is the code:

    add_filter('views_edit-text', 'keep_post_status_menu_options_visible');
    
    /**
     * Keep all 'post status' menu options visible all the time.
     */
    function keep_post_status_menu_options_visible($views) {
    
        $current_user = strval(get_current_user_id());
    
        $post_statuses = array("all" => "all_posts=1",
                               "mine" => "author={$current_user}",
                               "publish" => "post_status=publish",
                               "future" => "post_status=future",
                               "draft" => "post_status=draft",
                               "pending" => "post_status=pending",
                               "private" => "post_status=private",
                               "trash" => "post_status=trash");
    
        $post_statuses_translated = array("all" => "Alla",
                                          "mine" => "Mina",
                                          "publish" => "Publicerat",
                                          "future" => "Tidsinst?llt",
                                          "draft" => "Utkast",
                                          "pending" => "Inv?ntar granskning",
                                          "private" => "Privat",
                                          "trash" => "Papperskorg");
    
        foreach ($post_statuses as $status => $query_arg) {
            if (!isset($views[$status])) {
                $querystring = "post_type=text&{$query_arg}";
                $href = admin_url("edit.php?{$querystring}");
                $status_translation = $post_statuses_translated[$status];
                $selected = ($querystring === $_SERVER['QUERY_STRING']) ? "class='current'" : "";
                $link = "<a href='{$href}' {$selected}>{$status_translation}<span class='count'>(0)</span></a>";
                $views[$status] = $link;
            }
        }
    
        return $views;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Always keep ‘post status’ menu options visible in edit.php?’ is closed to new replies.