Skip to content

Commit

Permalink
feat: filter out password protected pages from listing pages
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-jw committed Sep 26, 2023
1 parent a969116 commit c3ee959
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
5 changes: 0 additions & 5 deletions src/php/archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
* The template for displaying archive pages
*/

$page_num = get_query_var( 'paged' );
$page_num = empty( $page_num ) ? 1 : $page_num;
$posts_per_page = 12;
$count = $GLOBALS['wp_query']->post_count;

$templates = array( 'archive.twig', 'index.twig' );

$context = Timber\Timber::context();
Expand Down
19 changes: 17 additions & 2 deletions src/php/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ function add_to_context( $context ) {
/**
* Render page content with password protection.
*
* @param object $post - The current post.
* @param object $post - The current post.
* @param string|array $templates - The template(s) to render.
* @param object $context - The Timber context used to render.
* @param object $context - The Timber context used to render.
*/
function render_with_password_protection( $post, $templates, $context ) {
if ( post_password_required( $post->ID ) ) {
Expand All @@ -96,3 +96,18 @@ function render_with_password_protection( $post, $templates, $context ) {
Timber::render( $templates, $context );
}
}

/**
* Filter password protected posts out of listing page queries.
*
* @param object $query - The incoming query.
* @return object
*/
function filter_password_protected_posts( $query ) {
if ( ! $query->is_admin && ! $query->is_single ) {
$query->set( 'has_password', false );
}

return $query;
}
add_filter( 'pre_get_posts', 'filter_password_protected_posts' );
4 changes: 3 additions & 1 deletion src/php/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

$context = Timber\Timber::context();
$context['title'] = 'Search results for ' . get_search_query();

$search_term = get_search_query();
$context['title'] = 'Search results for ' . $search_term;
$context['posts'] = new Timber\PostQuery();

Timber\Timber::render( 'search.twig', $context );
8 changes: 0 additions & 8 deletions wp-configs/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Expand Down

0 comments on commit c3ee959

Please sign in to comment.