Add Sticky post attribute to CPT UI

I’m trying to figure out how to define a post as either featured or sticky to display in a different way on my WordPress build.

CPT UI gave me:

Read More
add_action('init', 'cptui_register_my_cpt_projects');
function cptui_register_my_cpt_projects() {
register_post_type('projects', array(
'label' => 'Projects',
'description' => 'Add individual projects',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'projects', 'with_front' => true),
'query_var' => true,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
'labels' => array (
  'name' => 'Projects',
  'singular_name' => 'Project',
  'menu_name' => 'Projects',
  'add_new' => 'Add Project',
  'add_new_item' => 'Add New Project',
  'edit' => 'Edit',
  'edit_item' => 'Edit Project',
  'new_item' => 'New Project',
  'view' => 'View Project',
  'view_item' => 'View Project',
  'search_items' => 'Search Projects',
  'not_found' => 'No Projects Found',
  'not_found_in_trash' => 'No Projects Found in Trash',
  'parent' => 'Parent Project',
)
) ); }

I tried adding in 'meta_key' => 'Sticky', & 'meta_key' => 'on', however neither were successful.

How would I go about adding an option to the front end of each post to make it featured or sticky?

Related posts

Leave a Reply

1 comment

  1. As far as I’m aware custom post types are not supported for sticky posts. However I managed to get around it with this function.

    <?php 
    function wpb_cpt_sticky_at_top( $posts ) {
    
        // apply it on the archives only
        if ( is_main_query() && is_post_type_archive() ) {
            global $wp_query;
    
            $sticky_posts = get_option( 'sticky_posts' );
            $num_posts = count( $posts );
            $sticky_offset = 0;
    
            // Find the sticky posts
            for ($i = 0; $i < $num_posts; $i++) {
    
                // Put sticky posts at the top of the posts array
                if ( in_array( $posts[$i]->ID, $sticky_posts ) ) {
                    $sticky_post = $posts[$i];
    
                    // Remove sticky from current position
                    array_splice( $posts, $i, 1 );
    
                    // Move to front, after other stickies
                    array_splice( $posts, $sticky_offset, 0, array($sticky_post) );
                    $sticky_offset++;
    
                    // Remove post from sticky posts array
                    $offset = array_search($sticky_post->ID, $sticky_posts);
                    unset( $sticky_posts[$offset] );
                }
            }
    
            // Look for more sticky posts if needed
            if ( !empty( $sticky_posts) ) {
    
                $stickies = get_posts( array(
                    'post__in' => $sticky_posts,
                    'post_type' => $wp_query->query_vars['post_type'],
                    'post_status' => 'publish',
                    'nopaging' => true
                ) );
    
                foreach ( $stickies as $sticky_post ) {
                    array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
                    $sticky_offset++;
                }
            }
    
        }
    
        return $posts;
    }
    
    add_filter( 'the_posts', 'wpb_cpt_sticky_at_top' );
    
    // Add sticky class in article title to style sticky posts differently
    
    function cpt_sticky_class($classes) {
          if ( is_sticky() ) : 
          $classes[] = 'sticky';
              return $classes;
        endif; 
        return $classes;
            }
    add_filter('post_class', 'cpt_sticky_class');
    

    This will add a meta checkbox in all CPT posts that will say ‘Sticky’ and will float to the top in that CPT archive