Skip to content

feat(search-ordering): allow removing posts from custom search results#4327

Open
faisalahammad wants to merge 1 commit into
10up:developfrom
faisalahammad:fix/4245-custom-search-result-remove
Open

feat(search-ordering): allow removing posts from custom search results#4327
faisalahammad wants to merge 1 commit into
10up:developfrom
faisalahammad:fix/4245-custom-search-result-remove

Conversation

@faisalahammad

Copy link
Copy Markdown

Summary

Added ability to remove individual posts from a custom search result.
Excluded posts are now stored and filtered out from the final search results for that query.
Fixes #4245

Changes

Frontend (assets/js/ordering/pointers.js)

Before:

removePointer(pointer) {
    // Only removed reordered default results via removedPointers.
}

After:

removePointer(pointer) {
    if (pointer.type === 'custom-result') {
        excludedPosts.push(pointer.ID);
    } else {
        removedPointers.push(pointer.ID);
    }
    // Updates state and hidden input excluded_posts
}

Why: Differentiates between custom-added results (should be excluded) and reordered defaults (should be returned to original position).

Backend (includes/classes/Feature/SearchOrdering/SearchOrdering.php)

Before:

function save_post($post_id) {
    // Saved pointers only.
}
function posts_results($posts) {
    // Injected custom results but did not filter any posts.
}

After:

function save_post($post_id) {
    // Now reads $_POST['excluded_posts'], sanitizes, and stores in post meta 'excluded_posts'.
    // Clears excluded list when search term changes.
}
function posts_results($posts) {
    // After injecting custom results, fetches excluded list and removes those posts from results.
}

Why: Persists the exclusion list per custom search result and applies it to query results.

Tests (tests/php/features/TestSearchOrdering.php)

Added three new PHPUnit tests:

  • testSavePostWithExcludedPosts
  • testPostsResultsFiltersExcludedPosts
  • testExcludedPostsClearedOnSearchTermChange

Testing

Test 1: Exclude a post from a custom search result

  1. Create a custom search result for "example".
  2. Add a post to the result.
  3. Click the trash icon on the added post.
  4. Save the custom search result.
  5. Search for "example" on the frontend.
    Result: The excluded post does not appear in the search results.

Test 2: Automated tests pass
Run composer run test --filter TestSearchOrdering.
Result: All tests pass.

Fixes 10up#4245

- Adds excludedPosts tracking in the Custom Search Results UI.
- Persists excluded post IDs in ep-pointer post meta.
- Filters excluded posts from the final search results query.
- Adds PHPUnit tests for save, filter, and search-term change behaviors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove posts in Custom Search Result

1 participant