-
Notifications
You must be signed in to change notification settings - Fork 0
feat: exclude current post from query #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,29 @@ public function get(): Collection | |
| ->limit($this->attributes->limit()) | ||
| ->offset($this->attributes->offset()); | ||
|
|
||
| /** | ||
| * Filters whether the current post should be excluded from query results. | ||
| * | ||
| * This applies when the query is not using a manual selection and the current post is not the configured sticky post. | ||
| * | ||
| * @param bool $excludeCurrentPost Whether to exclude the current post from the query results. Default true. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ik weet niet of ik de @param en @return echt zinvol vindt en tussen de 2e eerste regels staan wat mij betreft een enter teveel. 🐜 |
||
| * | ||
| * @return bool Whether to exclude the current post from the query results. | ||
| */ | ||
| $excludeCurrentPost = apply_filters('yard_query_block_exclude_current_post', true); | ||
|
|
||
| $currentPostId = $this->currentPostId(); | ||
| if ( | ||
| $excludeCurrentPost && | ||
| ! $this->attributes->hasManualSelection() && | ||
| ( | ||
| ! $this->attributes->hasStickyPost() || | ||
| $this->attributes->stickyPostID() !== $currentPostId | ||
| ) && | ||
| 0 < $currentPostId) { | ||
| $query->where('ID', '!=', $currentPostId); | ||
| } | ||
|
|
||
| if ($this->attributes->hasManualSelection()) { | ||
| $query->whereIn('ID', $this->attributes->manualSelectionPostIDs()); | ||
| } | ||
|
|
@@ -175,4 +198,17 @@ private function order(PostBuilder $query): PostBuilder | |
|
|
||
| return $query; | ||
| } | ||
|
|
||
| private function currentPostId(): int | ||
| { | ||
| if (function_exists('get_the_ID') && $id = get_the_ID()) { | ||
| return $id; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| if (function_exists('get_queried_object_id')) { | ||
| return get_queried_object_id(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Voor de zekerheid misschien: |
||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.