Add the below code in function.php file
My Post Type Slug: products
/* Add Custom order in Custom Post Type */
function sm_custom_post_sort( $post ){
add_meta_box(
'custom_post_sort_box',
'Position in List of Products',
'sm_custom_post_order',
'products' ,
'side'
);
}
add_action( 'add_meta_boxes', 'sm_custom_post_sort' );
function sm_custom_post_order( $post ) {
wp_nonce_field( basename( __FILE__ ), 'sm_custom_post_order_nonce' );
$current_pos = get_post_meta( $post->ID, '_custom_post_order', true); ?>
Enter the position at which you would like the products to appear. For exampe, products "1" will appear first, products "2" second, and so forth.
<input type="number" name="pos" value="<?php echo $current_pos; ?>" />
<?php
}
/* Save the input to post_meta_data */
function sm_save_custom_post_order( $post_id ){
if ( !isset( $_POST['sm_custom_post_order_nonce'] ) || !wp_verify_nonce( $_POST['sm_custom_post_order_nonce'], basename( __FILE__ ) ) ){
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
if ( isset( $_REQUEST['pos'] ) ) {
update_post_meta( $post_id, '_custom_post_order', sanitize_text_field( $_POST['pos'] ) );
}
}
add_action( 'save_post', 'sm_save_custom_post_order' );
/* Add custom post order column to post list */
function sm_add_custom_products_post_order_column( $columns ){
return array_merge ( $columns,
array( 'pos' => 'Position', ));
}
add_filter('manage_products_posts_columns' , 'sm_add_custom_products_post_order_column');
/* Display custom post order in the post list */
function sm_custom_products_post_order_value( $column, $post_id ){
if ($column == 'pos' ){
echo '' . get_post_meta( $post_id, '_custom_post_order', true) . '
';
}
}
add_action( 'manage_products_posts_custom_column' , 'sm_custom_products_post_order_value' , 10 , 2 );
/* Sort posts on the blog posts page according to the custom sort order */
function sm_custom_post_order_sort( $query ){
if ( $query->is_main_query() && is_home() ){
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', '_custom_post_order' );
$query->set( 'order' , 'ASC' );
}
}
add_action( 'pre_get_posts' , 'sm_custom_post_order_sort' );
How to Fetch “custom_post_order” in Front Side?
<!-- Products Section Start Here -->
<?php
$products_type = 'products';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$products_args=array(
'type' => $products_type,
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged,
'caller_get_posts' => -1,
'child_of' => 0,
'parent' => 0,
'meta_key' => '_custom_post_order',
'orderby' => 'meta_value',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'product-category',
'field' => 'id',
'terms' => $product_category_term_id
)
),
'pad_counts' => false
);
$products_my_query = null;
$products_my_query = new WP_Query($products_args);
if( $products_my_query->have_posts() )
{
while ($products_my_query->have_posts()) : $products_my_query->the_post();
$product_description = get_the_excerpt($post->ID);
?>
<div class="product_box">
<div class="news_and_eve">
<?php the_title( '<h1>', '</h1>' );?>
</div>
<div class="product_content">
<?php
if ( has_post_thumbnail() ) {
?>
<div class="image_cont"><?php echo get_the_post_thumbnail($post->ID,"thumbnail"); //thumbnail,medium,large,full,array(100,100)?></div>
<?php
}
?>
<?php echo $product_description; ?>
</div>
<!-- Read more Here -->
<div class="read_more_btn">
<a href="<?php echo get_permalink(); ?>">Read More...</a>
</div>
</div>
<?php
endwhile;
}
wp_reset_query($products_my_query);
?>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $products_my_query ) ); }else{ echo "No Products Found.";}?>
<!-- Products Section Start Here -->
Recent Comments