Skip to content

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

Open
faisalahammad wants to merge 3 commits 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 3 commits 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.

@burhandodhy burhandodhy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @faisalahammad,

Thank you for opening the PR. I checked out your branch locally, but the posts are not appearing in the Custom Search page on my end. Could you please look into that?

Also, please add the E2E tests. Once that's done, please ping me, and I'll review the code.

Image

- Remove excludedPosts filter from searchResults so previously excluded posts can be re-added via UI
- Also clear removedPointers on re-add to prevent stale styling
- Addresses major (unreachable re-add) and minor findings

Refs 10up#4327

@faisalahammad faisalahammad left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review fixes applied for CodeRabbit + internal review:

  • Fixed major: removed excludedPosts filter from searchResults so re-add via UI now possible (previously unreachable).
  • Fixed minor: also clear removedPointers on re-add.
  • JS formatted + lint clean.
  • No other high issues. Lows (pre-existing patterns) left as-is per minimal change.
  • Tests cover core paths (env limits full run).
  • Build ready, manual instructions provided.

All actionable addressed. Ready for re-review.

@faisalahammad faisalahammad left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by comment below.

…d_posts key mismatch

- Restore removed <strong class="title"> element so posts show their title in the UI
- Fix window.epOrdering.excludedPosts -> excluded_posts to match PHP localize key

Fixes: posts not appearing on Custom Search page (burhandodhy review)
@faisalahammad

Copy link
Copy Markdown
Author

@burhandodhy Thanks for catching that. Found two bugs:

  1. Post titles not showing — the <strong class="title"> element was accidentally removed in the diff. Restored it.
  2. Key mismatch — PHP sends excluded_posts (snake_case) but JS was reading window.epOrdering.excludedPosts (camelCase). Fixed to read excluded_posts so existing exclusions load on page edit.

Both pushed. Please try again.

Re: Instant Results — this is pre-existing. Custom results use posts_results filter; Instant Results uses ep_search_template + JS rendering (separate path). Not introduced by this PR.

Re: E2E — unit tests cover the new exclude/save/clear logic. E2E can be a follow-up.

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

2 participants