Last active
July 31, 2017 14:30
-
-
Save igorbenic/f167b21e3e90172a75be39a62b6efdd4 to your computer and use it in GitHub Desktop.
How to Create Featured Downloads for Easy Digital Downloads | http://www.ibenic.com/create-featured-downloads-for-edd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Adding the Featured Class | |
| * @param array $classes | |
| * @return array | |
| */ | |
| function ibenic_edd_featured_class( $classes ) { | |
| global $post; | |
| // Code will go here | |
| return $classes; | |
| } | |
| add_filter( 'post_class', 'ibenic_edd_featured_class' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // ibenic_edd_featured_class( $classes ) | |
| if( 'download' == $post->post_type ) { | |
| $is_featured = (boolean) get_post_meta( $post->ID, '_edd_featured', true ); | |
| if( $is_featured ) { | |
| $classes[] = 'edd-featured'; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'edd_meta_box_settings_fields', 'ibenic_edd_featured_field', 20 ); | |
| /** | |
| * Adding the Featured Field to the Settings | |
| * @param integer $post_id | |
| * @return void | |
| */ | |
| function ibenic_edd_featured_field( $post_id ) { | |
| // Code will go here | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // ibenic_edd_featured_field( $post_id ) | |
| $featured = (bool) get_post_meta( $post_id, '_edd_featured', true ); | |
| ?> | |
| <div id="edd_featured_wrap"> | |
| <p><strong><?php _e( 'Featured Field:', 'ibenic_edd' ); ?></strong></p> | |
| <label for="_edd_featured"> | |
| <?php echo EDD()->html->checkbox( array( | |
| 'name' => '_edd_featured', | |
| 'current' => $featured, | |
| ) ); ?> | |
| <?php _e( 'Set this product as a featured one', 'ibenic_edd' ); ?> | |
| </label> | |
| </div> | |
| <?php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'pre_get_posts', 'ibenic_edd_query' ); | |
| /** | |
| * Editing WP Query (Main Query) | |
| * @param WP_Query $query | |
| * @return void | |
| */ | |
| function ibenic_edd_query( $query ) { | |
| if( is_post_type_archive( 'download' ) && $query->is_main_query() ) { | |
| // Code will go here | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // ibenic_edd_query | |
| $meta_query = array( | |
| 'relation' => 'OR', | |
| 'with_key' => array( | |
| 'key' => '_edd_featured', | |
| 'compare' => 'EXISTS', | |
| ), | |
| 'no_key' => array( | |
| 'key' => '_edd_featured', | |
| 'compare' => 'NOT EXISTS', | |
| ) | |
| ); | |
| $query->set( 'meta_query', $meta_query ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // ibenic_edd_query | |
| $orderby = array( | |
| 'with_key' => 'DESC', | |
| 'post_date' => 'DESC', | |
| ); | |
| $query->set( 'orderby', $orderby ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'edd_metabox_fields_save', 'ibenic_edd_featured_field_save' ); | |
| /** | |
| * Adding our Featured Field to be saved as EDD field | |
| * @param array $fields | |
| * @return array | |
| */ | |
| function ibenic_edd_featured_field_save( $fields ) { | |
| $fields[] = '_edd_featured'; | |
| return $fields; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment