Last active
April 14, 2020 15:14
-
-
Save nfsarmento/e166d8a951c20829de8018a54051a3cc to your computer and use it in GitHub Desktop.
How to create a featured post with metabox - WordPress
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 | |
| # Edit your CPT $args and add the below | |
| 'register_meta_box_cb' => 'prfx_featured_meta', |
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 | |
| /** | |
| * Adds a meta box to the post editing screen | |
| */ | |
| function prfx_featured_meta() { | |
| add_meta_box( 'prfx_meta', __( 'Featured Posts', 'prfx-textdomain' ), 'prfx_meta_callback', 'portfolio', 'normal', 'high' ); | |
| } | |
| add_action( 'add_meta_boxes', 'prfx_featured_meta' ); | |
| /** | |
| * Outputs the content of the meta box | |
| */ | |
| function prfx_meta_callback( $post ) { | |
| wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' ); | |
| $prfx_stored_meta = get_post_meta( $post->ID ); | |
| ?> | |
| <span class="prfx-row-title"><?php _e( 'Check if this is a featured post: ', 'prfx-textdomain' ); ?></span> | |
| <div class="prfx-row-content"> | |
| <label for="featured-checkbox"> | |
| <input type="checkbox" name="featured-checkbox" id="featured-checkbox" value="yes" <?php if ( isset ( $prfx_stored_meta['featured-checkbox'] ) ) checked( $prfx_stored_meta['featured-checkbox'][0], 'yes' ); ?> /> | |
| <?php _e( 'Featured Item', 'prfx-textdomain' ); ?> | |
| </label> | |
| </div> | |
| <?php | |
| } | |
| /** | |
| * Saves the custom meta input | |
| */ | |
| function prfx_meta_save( $post_id ) { | |
| // Checks save status - overcome autosave, etc. | |
| $is_autosave = wp_is_post_autosave( $post_id ); | |
| $is_revision = wp_is_post_revision( $post_id ); | |
| $is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false'; | |
| // Exits script depending on save status | |
| if ( $is_autosave || $is_revision || !$is_valid_nonce ) { | |
| return; | |
| } | |
| // “Quick Edit” > update clears out my custom meta values | |
| // https://wordpress.stackexchange.com/questions/4931/quick-edit-update-clears-out-my-custom-meta-values/#answer-96055 | |
| if (wp_verify_nonce( $_POST['_inline_edit'], 'inlineeditnonce' ) ) { | |
| return; | |
| } | |
| // Checks for input and saves - save checked as yes and unchecked at no | |
| if( isset( $_POST[ 'featured-checkbox' ] ) ) { | |
| update_post_meta( $post_id, 'featured-checkbox', 'yes' ); | |
| } else { | |
| update_post_meta( $post_id, 'featured-checkbox', 'no' ); | |
| } | |
| } | |
| add_action( 'save_post', 'prfx_meta_save' ); |
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 | |
| $args = array( | |
| 'post_type' => 'portfolio', | |
| 'meta_query' => array( | |
| array( | |
| 'key' => 'featured-checkbox', | |
| 'value' => 'yes' | |
| ) | |
| ) | |
| ); | |
| $featured_query = new WP_Query( $args ); | |
| ?> | |
| <?php | |
| while ( $featured_query->have_posts() ) : | |
| $featured_query->the_post(); | |
| ?> | |
| <div class="container"> | |
| <div class="item-image"> | |
| <?php if ( has_post_thumbnail()) : ?> | |
| <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" > | |
| <img src="<?php echo get_the_post_thumbnail_url( $portfolio->ID, 'full'); ?>"> | |
| </a> | |
| <?php endif; ?> | |
| </div> | |
| <h2><a href="<?php the_permalink() ?>" class="package-title"><?php the_title(); ?></a></h2> | |
| <?php the_excerpt( 'Read More' ); ?> | |
| <a href="<?php the_permalink(); ?>" class="read-more">Read More...</a> | |
| <hr> | |
| </div> | |
| <?php | |
| endwhile; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment