Pledge List: Display pledges in list with sort and search ability (#33)

* Use the post-type when choosing template partial
* Add pledge archive templates
* Style pledge list page
* Style individual pledge
* Use pledge featured image for the logo
* Fix aspect ratio of image
* Add real contributors to the pledges
* Enable sorting and searching of pledges
* Add pledge sorting markup
* Style the page header
* Restrict any pledges with no contributors from being shown on the frontend
* Update contributor count key name
This commit is contained in:
Kelly Dwan 2019-10-25 16:34:46 -04:00 committed by GitHub
parent f47024efda
commit 553247cbf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 398 additions and 10 deletions

View file

@ -9,6 +9,7 @@ use WordPressDotOrg\FiveForTheFuture\Email;
use WordPressDotOrg\FiveForTheFuture;
use WP_Error;
use const WordPressDotOrg\FiveForTheFuture\PledgeMeta\META_PREFIX;
defined( 'WPINC' ) || die();
@ -18,6 +19,7 @@ const CPT_ID = FiveForTheFuture\PREFIX . '_' . SLUG;
add_action( 'init', __NAMESPACE__ . '\register', 0 );
add_action( 'admin_menu', __NAMESPACE__ . '\admin_menu' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\filter_query' );
/**
* Register all the things.
@ -167,3 +169,53 @@ function send_pledge_verification_email( $pledge_id, $action_page_id ) {
$message
);
}
/**
* Filter query for archive & search pages to ensure we're only showing the expected data.
*
* @param WP_Query $query The WP_Query instance (passed by reference).
* @return void
*/
function filter_query( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
$contributor_count_key = META_PREFIX . 'pledge-confirmed-contributors';
// Set up meta queries to include the "valid pledge" check, added to both search and any pledge requests.
$meta_queries = (array) $query->get( 'meta_query' );
$meta_queries[] = array(
'key' => $contributor_count_key,
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC',
);
if ( CPT_ID === $query->get( 'post_type' ) ) {
$query->set( 'meta_query', $meta_queries );
}
// Searching is restricted to pledges only.
if ( $query->is_search ) {
$query->set( 'post_type', CPT_ID );
$query->set( 'meta_query', $meta_queries );
}
// Use the custom order param to sort the archive page.
if ( $query->is_archive && CPT_ID === $query->get( 'post_type' ) ) {
$order = isset( $_GET['order'] ) ? $_GET['order'] : '';
switch ( $order ) {
case 'alphabetical':
$query->set( 'orderby', 'name' );
$query->set( 'order', 'ASC' );
break;
case 'contributors':
$query->set( 'meta_key', $contributor_count_key );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' );
break;
}
}
}