Forum Replies Created

Viewing 9 replies - 46 through 54 (of 54 total)
  • Thread Starter ninsan

    (@jennyrigsjo)

    Hi @wpusermanager, thank you for your reply and for the Google reCAPTCHA recommendation. I will definitely look into it!

    Happy holidays!

    /jenny

    Thread Starter ninsan

    (@jennyrigsjo)

    Dear @bcworkz, thank you so much for your time and all your helpful suggestions. I explored all of the solutions you suggested, however in the end I removed the pre_get_posts and pre_get_comments actions and instead made use of two filters called widget_posts_args and widget_comments_args. These hooks target specifically the queries made by the default WordPress Recent Posts and Recent Comments widgets and, while it meant I had to scrap the use of my own custom widgets/shortcodes, it made things a whole lot simpler as I no longer have to worry about the callbacks potentially affecting other queries.

    Once again, thank you so much for your invaluable help!

    /jen

    Thread Starter ninsan

    (@jennyrigsjo)

    Hi @bcworkz, thank you so much for helping. ?? I did what you suggested and when I var_dumped the “query_vars” array of the WP_QUERY object I noticed that the “tax_query” key contained the following values:

    ["tax_query"]=> array(1) { [0]=> array(4) { ["taxonomy"]=> string(10) "visibility" ["terms"]=> string(8) "loggedin" ["field"]=> string(4) "slug" ["operator"]=> string(6) "NOT IN"}}

    Operator => NOT IN. Hmm.

    I looked through my code and I discovered that I am using the action ‘pre_get_posts’ to modify a list of recent posts displayed in another footer widget. The callback hooked to ‘pre_get_posts’ modifies the tax_query so that no recent posts with the term “loggedin” are displayed if the user is not logged in. I commented out the ‘pre_get_posts’ callback and, sure enough, the code in my ‘pre_get_comments’ callback started working normally. So there’s my culprit, it seems. But I really need both of the callbacks to work. Is there any way for the ‘pre_get_comments’ callback to override or reset the changes made to the posts query by the ‘pre_get_posts’ callback?

    Also, on a side note, the line of code in my ‘pre_get_comments’ callback (posted above) that sets the “post__not_in” parameter of the comment_query is incorrect. The correct way to do it is:

    $comment_query->query_vars['post__not_in'] = $posts;

    Just pointing this out in case anyone else tries to use the code in their own project.

    • This reply was modified 4 years, 3 months ago by ninsan.
    Thread Starter ninsan

    (@jennyrigsjo)

    Hello again, just thought I’d let the forum mods know I have decided to put this issue to rest for now, as I have more pressing matters to attend to. Thank you so much for trying to help, @bcworkz, I really appreciate it. ?? Unfortunately I didn’t get much further than you in trying to figure out how to make my custom widget work. I’ve put the matter aside for now, but I might continue working on it in the future. If I ever find a solution or need more help, I will make another post here in the forum.

    Cheers,
    Jen

    Thread Starter ninsan

    (@jennyrigsjo)

    Hi @bcworkz , thank you for helping. Here is the code used to add the ‘dashboard_quick_draft’ widget to the dashboard:

    add_action('admin_init', 'jr_setup_dashboard');
    
    function jr_setup_dashboard() {
    
        global $pagenow;
    
        if (!is_admin() || $pagenow !== 'index.php') {
            return;
        }
    
        if (!current_user_can('administrator') && !current_user_can('editor')) {
            remove_meta_box( 'dashboard_quick_press', 'dashboard', 'normal' );
            add_action('wp_dashboard_setup', 'add_custom_quick_draft_widget');
        }
    }
    
    /**
     * Add the custom Quick Draft widget to the admin dashboard.
     */
    function add_custom_quick_draft_widget() {
        wp_add_dashboard_widget('jr_dashboard_quick_press', __( 'Quick Draft' ), 'dashboard_quick_draft');
    }

    The ‘jr_setup_dashboard’ function actually contains more code than what is shown here, but I have chosen to remove it for the sake of simplicity and relevance.

    /jen

    Thread Starter ninsan

    (@jennyrigsjo)

    Great, thanks! ??

    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;
    }
    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? ??

    Thread Starter ninsan

    (@jennyrigsjo)

    Thanks for your reply, @wpusermanager. I’m looking forward to the update! ??

Viewing 9 replies - 46 through 54 (of 54 total)