Skip to content

Add methods to hide filled and expired posts#2449

Closed
anaemnesis wants to merge 20 commits intotrunkfrom
fix/hide-expired-jobs
Closed

Add methods to hide filled and expired posts#2449
anaemnesis wants to merge 20 commits intotrunkfrom
fix/hide-expired-jobs

Conversation

@anaemnesis
Copy link
Contributor

@anaemnesis anaemnesis commented Jun 6, 2023

Fixes #1884

Changes proposed in this Pull Request

  • Adds three new methods: (1) get_filled_job_listings(), (2), get_expired_job_listings and (3) maybe_hide_filled_expired_job_listings_from_search
  • The first two methods only run if the "Hide Filled Jobs" or "Hide Expired Jobs" options are enabled. If they aren't, an empty array is returned.
  • The third method returns early if the "Hide Filled Jobs" and "Hide Expired Jobs" options are disabled. If they aren't, a post__not_in parameter is applied to $query->set when not in WP Admin, and when on a WP search results page.

Unlike the previous implementation, this one relies on get_posts() and meta_query but without the expensive OR relation.

Because expired jobs already have their post status updated to expired if a post's expiry date is set in the past or elapses, we don't need to meta_query for expired posts but can instead look for job_listings with an expired post status.

We don't have the same luxury with filled posts, but we can do a singular meta_query without any relations to retrieve these.

My understanding is that these changes should be far less expensive than the previous implementation.

To further help with performance, the results of get_filled_job_listings and get_expired_job_listings are stored in a transient that is expired every 24 hours, which lines up with the various job expiration methods.

Either transient is then cleared on post update if: (1) we're looking at a job listing, (2) the '_filled' post meta has been updated, or (3) the post status has changed from publish to expired, trash or vice-versa.

Testing instructions

  • Check out branch
  • Create a few job posts, setting one or some to Expired with an expiry date in the past, or Filled by checking the Filled checkbox.
  • Check transients to ensure hide_filled_jobs_transient and hide_expired_jobs_transient is not set. With WP CLI these can be checked with wp transient get hide_filled_jobs_transient and wp transient get hide_expired_jobs_transient
  • Navigate to Job Listings --> Settings --> Job Listings and enable (as desired), the Hide Filled Positions and Hide expired listings in your job archives/search options
  • Check your transient again, which should now be populated with the post IDs of the posts you marked as expired or filled
  • Check WP Search results to make sure these posts, and only these posts, are excluded

If you would like to test on a large dataset, WP CLI can be used to bulk create posts:

Bulk create Expired posts

wp post generate --count=1000 --post_type=job_listing --post_status=expired

Bulk generate Filled posts

Filled posts are a little more troublesome because we're working with meta values, and wp post generate, as well as wp post create, do not touch these values.

So, you could do something like:

# generate our posts
wp post generate --count=1000 --post_type=job_listing --post_status=publish

# grab the IDs of some arbitrary number of posts, say, 100, and update the post meta
for post_id in $(wp post list --post_type=job_listing --post_status=publish --numberposts=100 --fields=ID); do
    wp post meta update \
        $post_id \
        _filled \
        1 \
       && echo "Post ID $post_id changed"
done

The above can be added to a file on your environment, like create-filled-posts.sh. Make sure to chmod +x $file and then ./$file.sh to execute the for loop and create your posts.

Note that when creating posts programmatically you may need to manually clear the transients.

Loading
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.

WordPress search returns expired job

5 participants