alleyinteractive / homepages

0 stars 1 forks source link

Only show published homepages #6

Closed renatonascalves closed 3 years ago

renatonascalves commented 3 years ago

This fixes a bug where if you don't have a published homepage but you have a draft one, the front-end just fails and shows a not found page.

A common scenario exists when one is crafting a new homepage. Before, the front-end would just break.

Ticket created after the fact: https://alleyinteractive.atlassian.net/browse/DELTA-975

renatonascalves commented 3 years ago

@kjbenk I saw the homepages_modify_main_query hook but I thought we should have an out of box solution, without having to resort to code.

The scenario I encountered would make this hook more troublesome than necessary. Push code to disable the query, create a homepage, publish it, and then push code again to reactivate it?

That seems like the wrong way to handle this problem.

kjbenk commented 3 years ago

@renatonascalves I had some more time to think about this and I see the issue you are describing more clearly now. What I think we should do is:

  1. Implement this out of the box solution to not modify the main query when there is no published homepage.
  2. Cache the bool value for whether or not we have a homepage within an option (i.e. update_option). This way we do not need to perform an extra query on the frontend of the site, we only need to check an option value.
  3. Update the option value when a homepage is saved (or maybe only when a homepage is published). This means the query only happens on the backend and then frontend can remain performant.
  4. Update the has_homepage function to just return the value of this option.
return (bool) get_option( 'has_published_homepage', false );

This should keep things very performant while also ensure we are not modifying the query when there is not a published homepage.

renatonascalves commented 3 years ago

@kjbenk This is a much better solution. The only downside is that the option keeps there in the database even after the last created homepage is deleted. It is not a big deal though and it doesn't affect the homepage (custom or otherwise).