Quantcast
Channel: WordPress.org Forums » [Search Exclude] Support
Viewing all articles
Browse latest Browse all 100

Feature Request: Add a filter link for Search-Excluded pages

$
0
0

Replies: 0

Hi,

I would like to make a Feature Request. Basically just a simple filter function. I build an extension that works for me, but it would be nice to have this natively built into the plugin.

Here is the code I used:

<?php
/**
* Add a filter link for Search-Excluded pages in the admin area
*/
function add_excluded_pages_filter()
{
// Only run on the pages list screen
$screen = get_current_screen();
if ($screen->id !== 'edit-page') {
return;
}

// Get the settings from the Search Exclude plugin
$settings = get_option('qlse_settings', array());

// Check if we have excluded pages
$excluded_pages = array();
if (isset($settings['entries']['page']['ids']) && is_array($settings['entries']['page']['ids'])) {
$excluded_pages = $settings['entries']['page']['ids'];
}

// Count the excluded pages
$excluded_count = count($excluded_pages);

// Get the current filter status
$is_excluded_filter = isset($_GET['search_excluded']) && $_GET['search_excluded'] == '1';

// Only show if there are some excluded pages
if ($excluded_count > 0) {
// Inject the link with JavaScript after "Cornerstone-Inhalt (0)"
?>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
// Create the URL in JavaScript instead of PHP to avoid encoding issues
var adminUrl = '<?php echo admin_url('edit.php'); ?>';
var isFiltered = <?php echo $is_excluded_filter ? 'true' : 'false'; ?>;

// Build the URL directly in JavaScript
var excludedUrl = adminUrl + '?post_type=page';
if (!isFiltered) {
excludedUrl += '&search_excluded=1';
}

// Find the .subsubsub element
var subsubsub = document.querySelector('.subsubsub');
if (subsubsub) {
// Create a text node for " | "
var separator = document.createTextNode(' | ');

// Create our excluded link
var excludedLink = document.createElement('a');
excludedLink.href = excludedUrl;
excludedLink.innerHTML = '<?php echo esc_html__('Excluded', 'search-exclude') . ' (' . $excluded_count . ')'; ?>';
<?php if ($is_excluded_filter): ?>
excludedLink.className = 'current';
<?php endif; ?>

// Append our elements to the end of .subsubsub
subsubsub.appendChild(separator);
subsubsub.appendChild(excludedLink);
}
});
</script>
<?php
}
}
add_action('restrict_manage_posts', 'add_excluded_pages_filter', 100);

/**
* Modify the query when the excluded filter is selected
*/
function filter_pages_by_search_excluded($query)
{
// Only run on admin page listings
if (!is_admin() || !$query->is_main_query()) {
return;
}

// Make sure we're on the pages screen
$screen = get_current_screen();
if (!$screen || $screen->id !== 'edit-page') {
return;
}

// Check if our filter is active
if (isset($_GET['search_excluded']) && $_GET['search_excluded'] == '1') {
// Get the settings from the Search Exclude plugin
$settings = get_option('qlse_settings', array());

// Get excluded page IDs - access the correct structure
$excluded_pages = array();
if (isset($settings['entries']['page']['ids']) && is_array($settings['entries']['page']['ids'])) {
$excluded_pages = $settings['entries']['page']['ids'];
}

// Make sure we have excluded pages to filter
if (!empty($excluded_pages)) {
// Set the post__in parameter to show only excluded pages
$query->set('post__in', $excluded_pages);
} else {
// If no pages are excluded but the filter is active, show no results
$query->set('post__in', array(0)); // This will return no posts
}
}
}
add_action('pre_get_posts', 'filter_pages_by_search_excluded', 10);

Result:


Viewing all articles
Browse latest Browse all 100

Trending Articles