/** * function ffpw_contact_page_checkbox_save() * saves the checkbox marking a page as the contact page */ function ffpw_featured_post_checkbox_save($post_id) { /* only do this for ffpw post types */ if (!in_array(get_post_type($post_id), ffpw_post_types())) { return; } /* check if our nonce is se */ if (!isset($_POST['ffpw_featured_post_checkbox_nonce'])) { return; } /* verify that the nonce is valid */ if (!wp_verify_nonce($_POST['ffpw_featured_post_checkbox_nonce'], 'ffpw_featured_post_checkbox')) { return; } /* if this is an autosave, our form has not been submitted, so we don't want to do anything */ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } /* check the current user can edit this post */ if (!current_user_can('edit_post', $post_id)) { return; } /* if the box has beeb ticked */ if (isset($_POST['ffpw_featured_post'])) { /* update post meta with the contact page value */ update_post_meta($post_id, '_ffpw_featured', sanitize_text_field($_POST['ffpw_featured_post'])); } }
/** * builds the widget front end output * including the before and after parts */ function widget($args, $instance) { $featured_post_query_args = array('post_type' => ffpw_post_types(), 'posts_per_page' => 1, 'meta_key' => '_ffpw_featured', 'meta_value' => 1); /* check whether we should be showing a random featured post rather than the latest one */ if ($instance['random_post'] == 1) { /* add to our query arg for getting a random post */ $featured_post_query_args['orderby'] = 'rand'; } /* setup a query to get the featured post */ $ffpw_post = new WP_Query(apply_filters('ffpw_featured_post_query_args', $featured_post_query_args)); /* check if we have featured posts to show */ if ($ffpw_post->have_posts()) { /* output the markup set before the widget */ echo $args['before_widget']; /* loop through the post */ while ($ffpw_post->have_posts()) { $ffpw_post->the_post(); global $post; /** * @hook ffpw_featured_post_output * $param $title is the title to output * $param $post is the object of the queried featured post * @param $instance is an array of the widget instances * @param $args is an array of the widget arguments e.g. before_widget * * do widget action to hook output to */ do_action('ffpw_featured_post_output', $args, $instance, $post); /* end loop */ } /* output the after widget markup */ echo $args['after_widget']; } // end if have featured post /* reset query */ wp_reset_query(); }