Find answers, ask questions, and connect with our community.

Timeline Forums UK Website Q&A (and WordPress) Restricting Blog to Featured Posts

  • Restricting Blog to Featured Posts

    Posted by profmag on March 2, 2021 at 8:30 pm

    I wanted the blog and archive to be restricted to featured content using a meta-field. This allows our users to post content for the community that’s not featured to the public. The ability to select posts to feature is limited to admins. I used these instructions and adapted it using the following snippets:

    In my snippet manager (could also be added to functions.php):

    <?php
    if ( current_user_can('moderate_comments') )
    {
    function sm_custom_meta() {
    add_meta_box( 'sm_meta', __( 'Featured Posts', 'sm-textdomain' ), 'sm_meta_callback', 'post' );
    }
    function sm_meta_callback( $post ) {
    $featured = get_post_meta( $post->ID );
    ?>

    <p>
    <div class="sm-row-content">
    <label for="meta-checkbox">
    <input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $featured['meta-checkbox'] ) ) checked( $featured['meta-checkbox'][0], 'yes' ); ?> />
    <?php _e( 'Featured this post', 'sm-textdomain' )?>
    </label>

    </div>
    </p>

    <?php
    }
    add_action( 'add_meta_boxes', 'sm_custom_meta' );
    }
    ?>
    <?php 
    /**
    * Saves the custom meta input
    */
    function sm_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'sm_nonce' ] ) && wp_verify_nonce( $_POST[ 'sm_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
    if( isset( $_POST[ 'meta-checkbox' ] ) ) {
    update_post_meta( $post_id, 'meta-checkbox', 'yes' );
    } else {
    update_post_meta( $post_id, 'meta-checkbox', '' );
    }

    }
    add_action( 'save_post', 'sm_meta_save' );

    Then in index.php / archive.php:

                    /* Start the Loop */
    $args = array(
    'posts_per_page' => 5,
    'meta_key' => 'meta-checkbox',
    'meta_value' => 'yes'
    );
    $featured = new WP_Query($args);
    while ($featured->have_posts() ) :
    $featured->the_post();
    /*
    * Include the Post-Format-specific template for the content.
    * If you want to override this in a child theme, then include a file
    * called content-___.php (where ___ is the Post Format name) and that will be used instead.
    *
    * I am not sure why we need to laod content-post_format.php only if blog_type is not standard
    * lets load that in all cases.
    * Please change this if required.
    */
    get_template_part( 'template-parts/content', apply_filters( 'bb_blog_content', get_post_format() ) );
    endwhile;
    profmag replied 5 years, 3 months ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

Log in to reply.