• Resolved kcinn

    (@kcinn)


    I would like the Search Exclude checkbox to be checked off as a default for Posts (posts only- not Pages). I’m assuming there is a snippet of code I’ll need to add in or change in the search-exclude.php or search_exclude.js file but I’m having trouble sorting through it all. Any help would be appreciated

    https://ww.wp.xz.cn/plugins/search-exclude/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor pronskiy

    (@pronskiy)

    Hi,

    Let me think about possible solution and I’ll come back to you soon.

    Regards,
    Roman

    Plugin Contributor pronskiy

    (@pronskiy)

    If I got your problem right, you want following:
    – When user creates new post, the “Exclude from Search Results” checkbox should be checked by default

    Here is the solution I found so far:

    • Edit the file wp-content/plugins/search-exclude/search-exclude.php
    • Find line
      add_action('admin_enqueue_scripts', array($this, 'addStyle'));
    • After that line add new line:
      add_filter( 'default_content', array($this, 'excludeNewPostByDefault'), 10, 2);
    • Add new function to the class:
      public function excludeNewPostByDefault($content, $post)
          {
              if ('post' === $post->post_type) {
                  $this->savePostIdToSearchExclude($post->ID, true);
              }
          }

    To hide all existing posts you can use bulk actions.

    Please try this solution and let me know if it worked for.
    I will implement more elegant solution in the next plugin update.

    Best regards,
    Roman

    Thread Starter kcinn

    (@kcinn)

    Yes this worked perfectly. The reason for this was that drafted posts were showing up in the search results even though they weren’t published yet. Thanks for the solution!

    Thread Starter kcinn

    (@kcinn)

    On the flip side, is there a way to make it so that the Search Exclude checkbox becomes unchecked once the draft is published by an admin (without having to manually uncheck it)?

    Plugin Contributor pronskiy

    (@pronskiy)

    Let me please clarify what you are trying to achieve.

    – If admin publishes the draft – make checkbox unchecked.
    – If other users publish any posts – make checkbox checked off.

    Is my understanding correct?

    Thread Starter kcinn

    (@kcinn)

    I want it to be checked when the Contributor has it in draft mode and when it has been Submitted for Review (to avoid an unreviewed/unpublished draft from showing up in the global search results on the home page). I would like for the checkbox to be unchecked when it is published by an Administrator (so that the reviewed/published post does show up in the global search results on the home page). The point being making these checkbox actions the default so that users don’t need to take any extra manual steps. Thanks for any help you can provide.

    Plugin Contributor pronskiy

    (@pronskiy)

    If I got your requirements correctly, then this solution should work for you:

    • Edit the file wp-content/plugins/search-exclude/search-exclude.php
    • Find line
      add_action('admin_enqueue_scripts', array($this, 'addStyle'));
    • After that line add new line:

      add_action('draft_to_publish', array($this, 'draftToPublish'));
    • Add new function to the class:

      public function draftToPublish($post)
          {
              if (current_user_can('administrator')) {
                  $this->savePostIdToSearchExclude($post->ID, false);
                  unset($_POST['sep']);
              }
          }

    Please let me know if this worked for you.

    I wonder though why drafts are shown in global search for you? Shouldn’t they be hidden by default without plugin?

    Plugin Contributor pronskiy

    (@pronskiy)

    Hi,

    I have updated plugin and now since version 1.2.2 this issues can be solved without touching plugin itself.
    For the first one, you’ll nee to add following code to your theme”s functions.php:

    add_filter('default_content', 'excludeNewPostByDefault', 10, 2);
    function excludeNewPostByDefault($content, $post)
    {
    	if ('post' === $post->post_type) {
            do_action('searchexclude_hide_from_search', array($post->ID), true);
    	}
    }

    And for the second one:

    add_action('draft_to_publish', 'draftToPublish');
    function draftToPublish($post)
    {
        if (current_user_can('administrator')) {
            do_action('searchexclude_hide_from_search', array($post->ID), false);
            unset($_POST['sep']);
        }
    }

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Make Search Exclude checked by default’ is closed to new replies.