Skip to content

Instantly share code, notes, and snippets.

@jpen365
jpen365 / wp_widget::__construct().php
Created November 16, 2016 02:51
Use __construct() to define basic widget information.
<?php
public function __construct() {
$widget_options = array(
'classname' => 'example_widget',
'description' => 'This is an Example Widget',
);
parent::__construct( 'example_widget', 'Example Widget', $widget_options );
}
?>
@jpen365
jpen365 / wp_widget-instance.php
Last active November 16, 2016 16:44
Create a new instance of WP_Widget
<?php
class jpen_Example_Widget extends WP_Widget {
/**
* To create the example widget all four methods will be
* nested inside this single instance of the WP_Widget class.
**/
}
?>
<?php
/*
* First Loop
* Returns posts with a custom post order value
*/
// First, determine if on the first page of posts
// If on first page, run query to display posts with custom sort first
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
<?php
$args = array(
'post_type' => 'post',
'cat' => '94',
'meta_key' => '_custom_post_order',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$query = new WP_query ( $args );
<?php
/* Sort posts on the blog posts page according to the custom sort order */
function jpen_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' , 'jpen_custom_post_order_sort' );
<?php
/* Display custom post order in the post list */
function jpen_custom_post_order_value( $column, $post_id ){
if ($column == 'pos' ){
echo '<p>' . get_post_meta( $post_id, '_custom_post_order', true) . '</p>';
}
}
add_action( 'manage_posts_custom_column' , 'jpen_custom_post_order_value' , 10 , 2 );
?>
<?php
/* Add custom post order column to post list */
function jpen_add_custom_post_order_column( $columns ){
return array_merge ( $columns,
array( 'pos' => 'Position', ));
}
add_filter('manage_posts_columns' , 'jpen_add_custom_post_order_column');
?>
<?php
/* Save the input to post_meta_data */
function jpen_save_custom_post_order( $post_id ){
if ( !isset( $_POST['jpen_custom_post_order_nonce'] ) || !wp_verify_nonce( $_POST['jpen_custom_post_order_nonce'], basename( __FILE__ ) ) ){
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
<?php
/* Add a field to the metabox */
function jpen_custom_post_order( $post ) {
wp_nonce_field( basename( __FILE__ ), 'jpen_custom_post_order_nonce' );
$current_pos = get_post_meta( $post->ID, '_custom_post_order', true); ?>
<p>Enter the position at which you would like the post to appear. For exampe, post "1" will appear first, post "2" second, and so forth.</p>
<p><input type="number" name="pos" value="<?php echo $current_pos; ?>" /></p>
<?php
}
<?php
/* Create custom meta data box to the post edit screen */
function jpen_custom_post_sort( $post ){
add_meta_box(
'custom_post_sort_box',
'Position in List of Posts',
'jpen_custom_post_order',
'post' ,
'side'