Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Last active April 29, 2021 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/d9760f7536b8ac856225a28ff22db2a4 to your computer and use it in GitHub Desktop.
Save nfsarmento/d9760f7536b8ac856225a28ff22db2a4 to your computer and use it in GitHub Desktop.
Feature post metabox and query
<div class="posts-listings">
<?php
$args = array(
'posts_per_page' => '1',
'orderby' => 'featured-checkbox',
'order' => 'ASC',
'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="posts-items">
<div class="item-image">
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
</div>
<a href="<?php the_permalink(); ?>" class="posts-title">
<h2><?php the_title(); ?></h2>
</a>
<?php the_excerpt(); ?>
<a href="<?php the_permalink() ?>" class="read-more">Read More...</a>
</div>
<?php endwhile; ?>
</div>
<div class="posts-listings">
<!--Add all non-featured posts-->
<?php
$args = array(
'posts_per_page' => '1',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'featured-checkbox',
'compare' => 'NOT EXISTS', //or "NOT EXISTS", for non-existance of this key
)
)
);
$page_query = new WP_Query($args);
?>
<?php
while ($page_query->have_posts()) : $page_query->the_post();
?>
<div class="posts-items">
<!--DO MORE STUFF HERE -->
</div>
<?php
/**
* Adds a meta box to the post editing screen
*/
function ns_featured_meta() {
add_meta_box( 'ns_meta', __( 'Featured Posts', 'ns-domain' ), 'ns_meta_callback', 'post', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'ns_featured_meta' );
/**
* Outputs the content of the meta box
*/
function ns_meta_callback( $post ) {
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<span class="row-title"><?php _e( 'Check if this is a featured post: ', 'ns-domain' )?></span>
<div class="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', 'ns-domain' )?>
</label>
</div>
</p>
<?php
}
/**
* Saves the custom meta input
*/
function ns_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[ 'ns_nonce' ] ) && wp_verify_nonce( $_POST[ 'ns_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
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', 'ns_meta_save' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment