I added a custom column to WordPress admin user grid area by adding this codes to functions.php
function new_modify_user_table( $column ) {
$column['progress'] = 'Progress';
return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table',11 );
function new_modify_user_table_row( $val, $column_name, $user_id ) {
switch ($column_name) {
case 'progress' :
$course_id=7238;
$course = learn_press_get_course( $course_id );
$force = true;
$num_of_decimal = 0;
$current = $course->evaluate_course_results( $user_id, $force );
$current = absint( $current );
return '<strong style="color: green">'.$current.'%</strong>';
break;
default:
}
return $val;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 11, 3 );
It shows the column at the end of table as below
But it doesn't allow me to sort.
I added this code to add sort link to table header column
add_filter( 'manage_users_sortable_columns', 'my_sortable_cake_column' );
function my_sortable_cake_column( $columns ) {
$columns['progress'] = 'progress';
return $columns;
}
And also found where to trigger sort
add_action( 'pre_user_query', 'my_pre_user_query', 1 );
function my_pre_user_query( $query ) {
global $wpdb, $current_screen;
// Only filter in the admin
if ( ! is_admin() )
return;
// Only filter on the users screen
if ( ! ( isset( $current_screen ) && 'users' == $current_screen->id ) )
return;
// Only filter if orderby is set to 'progress'
if ( isset( $query->query_vars ) && isset( $query->query_vars[ 'orderby' ] )
&& ( 'progress' == $query->query_vars[ 'orderby' ] ) ) {
// We need the order - default is ASC
$order = isset( $query->query_vars ) && isset( $query->query_vars[ 'order' ] ) && strcasecmp( $query->query_vars[ 'order' ], 'desc' ) == 0 ? 'DESC' : 'ASC';
//need to write code to apply to grid
}
}
can someone help me write a code which set the sort to the grid
Related
I would like to only show orders from specific product or category to a certain shop manager with the ID: 5. I have found some code here. I have found and made the following, but its not working for me?
function before_checkout_create_order( $order, $data ) {
$product = $order->product_id;
$post_id = ['9534', '9532'];
if( in_array( $product, $post_id ) ) {
$store_manager_id = 5;
$order->update_meta_data('_store_manager_id', $store_manager_id);
}
}
add_action( 'woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2 );
function store_manager_orders_query( $query ) {
global $pagenow, $typenow;
$store_manager_id = 5;
if( !$query->is_main_query() ) {
return;
}
if( get_current_user_id() === $store_manager_id && $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $typenow ){
// Get and alter meta query
$meta_query = (array)$query->get('meta_query');
$meta_query[] = array(
'key' => '_store_manager_id',
'value' => $store_manager_id,
'compare' => '=',
);
// Set altered meta query
$query->set('meta_query',$meta_query);
}
}
add_action( 'pre_get_posts', 'store_manager_orders_query', 20 );
I'm using the WP Profile Builder plugin. In the frontend user listing I set a default sorting criteria. Works fine! But now I would like to have a second criteria that comes right after the default one.
Lets say first are the "gold" users and behind them the "silver" users.
Right now it looks like this:
*/
function wppb_userlisting_users_loop( $value, $name, $children, $extra_values ){
if( $name == 'users' ){
global $userlisting_args;
global $wpdb;
$userlisting_form_id = $extra_values['userlisting_form_id'];
$userlisting_args = get_post_meta( $userlisting_form_id, 'wppb_ul_page_settings', true );
if( !empty( $userlisting_args[0] ) ){
$paged = (get_query_var('wppb_page')) ? get_query_var('wppb_page') : 1;
if( !is_int( (int)$userlisting_args[0]['number-of-userspage'] ) || (int)$userlisting_args[0]['number-of-userspage'] == 0 )
$userlisting_args[0]['number-of-userspage'] = 5;
// Check if some of the listing parameters have changed
if ( isset( $_REQUEST['setSortingOrder'] ) && sanitize_text_field( $_REQUEST['setSortingOrder'] ) !== '' )
$sorting_order = sanitize_text_field( $_REQUEST['setSortingOrder'] );
else
$sorting_order = $userlisting_args[0]['default-sorting-order'];
/* if we have admin approval on we don't want to show those users in the userlisting so we need to exclude them */
if( wppb_get_admin_approval_option_value() === 'yes' ){
$excluded_ids = array();
$user_statusTaxID = get_term_by( 'name', 'unapproved', 'user_status' );
if( $user_statusTaxID != false ){
$term_taxonomy_id = $user_statusTaxID->term_taxonomy_id;
$results = $wpdb->get_results( $wpdb->prepare( "SELECT wppb_t1.ID FROM $wpdb->users AS wppb_t1 LEFT OUTER JOIN $wpdb->term_relationships AS wppb_t0 ON wppb_t1.ID = wppb_t0.object_id WHERE wppb_t0.term_taxonomy_id = %d", $term_taxonomy_id ) );
foreach ( $results as $result )
array_push( $excluded_ids, $result->ID );
$excluded_ids = implode( ',', $excluded_ids );
}
}
if( !empty($excluded_ids) )
$extra_values['exclude'] .= ','. $excluded_ids;
//set query args
$args = array(
'order' => $sorting_order,
'include' => $extra_values['include'],
'exclude' => $extra_values['exclude'],
'fields' => array( 'ID' )
);
/* get all field options here, we will need it bellow */
global $wppb_manage_fields;
if( !isset( $wppb_manage_fields ) )
$wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
// Check if some of the listing parameters have changed
if ( isset( $_REQUEST['setSortingCriteria'] ) && sanitize_text_field( $_REQUEST['setSortingCriteria'] ) !== '' )
$sorting_criteria = sanitize_text_field( $_REQUEST['setSortingCriteria'] );
else
$sorting_criteria = $userlisting_args[0]['default-sorting-criteria'];
if( in_array( $sorting_criteria, array( 'login', 'email', 'url', 'registered', 'post_count', 'nicename' ) ) ){
if( $sorting_criteria == 'nicename' )
$args['orderby'] = 'display_name';
}
else{
$args['orderby'] = apply_filters( 'wppb_ul_sorting_type', 'meta_value', $sorting_criteria );
if ($wppb_manage_fields != 'not_found') {
foreach ($wppb_manage_fields as $wppb_field) {
if( $wppb_field['meta-name'] == $sorting_criteria ){
if( $wppb_field['field'] == 'Number' || $wppb_field['field'] == 'Phone' ){
$args['orderby'] = apply_filters( 'wppb_ul_sorting_type', 'meta_value_num', $sorting_criteria );
}
}
}
}
switch( $sorting_criteria ){
case "bio":
$args['meta_key'] = 'description';
break;
case "firstname":
$args['meta_key'] = 'first_name';
break;
case "lastname":
$args['meta_key'] = 'last_name';
break;
case "nickname":
$args['meta_key'] = 'nickname';
break;
case "role":
$args['meta_key'] = $wpdb->get_blog_prefix().'capabilities';
break;
case "RAND()":
break;
default:
$args['meta_key'] = $sorting_criteria;
}
}
/* the relationship between meta query is AND because we need to narrow the result */
$args['meta_query'] = array('relation' => 'AND');
/* we check if we have a meta_value and meta_key in the shortcode and add a meta query */
if( !empty( $extra_values['meta_value'] ) && !empty( $extra_values['meta_key'] ) ){
$args['meta_query'][0] = array( 'relation' => 'AND' ); //insert relation here
$args['meta_query'][0][] = array(
'key' => $extra_values['meta_key'],
'value' => $extra_values['meta_value'],
'compare' => apply_filters( 'wppb_ul_meta_att_in_shortcode_compare', '=', $extra_values )
);
}
Can someone help me out with that? Best regards, Ingo
I already tried to change some parameters and checked what was changing but no luck for me!
I am trying to sort all my orders in alphabetical order in a custom column that I have built. Right now it can only be sorted asc and desc by the time when order was created.
I have built a custom column for user last name, I have passed billing last name inside that column.
Right now I am tryin to make it sort in alphabetical order. I am using the following:
function add_order_new_column_header( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_total' === $column_name ) {
$new_columns['billing_info'] = __( 'Billing Info', 'my-textdomain' );
}
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'add_order_new_column_header', 20);
add_action( 'manage_shop_order_posts_custom_column', 'add_wc_order_admin_list_column_content' );
function add_wc_order_admin_list_column_content( $column ) {
global $post;
if ( 'billing_info' === $column ) {
$order = wc_get_order( $post->ID );
echo '<p>' . $order->get_billing_last_name() . '</p>';
}
}
add_filter( 'manage_edit-shop_order_sortable_columns', 'my_sortable_name_column' );
function my_sortable_name_column( $columns ) {
$columns['billing_info'] = 'billing_last_name';
return $columns;
}
add_action( 'pre_get_posts', 'name_filter' );
function name_filter( $query ) {
// if it is not admin area, exit the filter immediately
if ( ! is_admin() ) return;
$orderby = $query->get( 'orderby');
if( $orderby == 'billing_info' ) {
$query->set('meta_key', 'billing_last_name' );
$query->set('orderby', 'meta_value');// or meta_value_num
$query->set('order', 'ASC');
}
return $query;
}
The custom column is sortable, so that works but the sort order I want is not applied in a correct way. Someone who can tell me why?
You have some minor bugs in your pre_get_posts function, also the correct meta key is _billing_last_name, not billing_last_name.
So use the following instead:
Explanation via comment tags added in the code
// Add a header
function filter_manage_edit_shop_order_columns( $columns ) {
$new_columns = array();
// Loop trough columns
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
// Compare, add after
if ( $column_name === 'order_total' ) {
$new_columns['billing_info'] = __( 'Billing info', 'woocommerce' );
}
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
// Populate the custom column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'billing_info' ) {
// Get an instance of the WC_Order object from an Order ID
$order = wc_get_order( $post_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Get billing last name
$billing_last_name = $order->get_billing_last_name();
// NOT empty
if ( ! empty ( $billing_last_name ) ) {
echo '<p>' . $billing_last_name . '</p>';
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );
// Make custom column sortable
function filter_manage_edit_shop_order_sortable_columns( $sortable_columns ) {
return wp_parse_args( array( 'billing_info' => '_billing_last_name' ), $sortable_columns );
}
add_filter( 'manage_edit-shop_order_sortable_columns', 'filter_manage_edit_shop_order_sortable_columns', 10, 1 );
// Orderby for custom column
function action_pre_get_posts( $query ) {
// If it is not admin area, exit
if ( ! is_admin() ) return;
global $pagenow;
// Compare
if ( $pagenow === 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] === 'shop_order' ) {
// Get orderby
$orderby = $query->get( 'orderby' );
// Set query
if ( $orderby == '_billing_last_name' ) {
$query->set( 'meta_key', '_billing_last_name' );
$query->set( 'orderby', 'meta_value' );
}
}
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );
Im using this code in displaying the sum of the product variable stock quantities from a parent variable products based on:
Display the stock availability for all product types in Woocommerce archive pages.
How can I make the sorting options to look at in the above script rather than the _stock meta? here is the script that i am using to sort the products.
protected function get_settings_options() {
$options = array(
'by_stock' => __( 'Available Stock', 'woocommerce-extra-product-sorting-options' ),
);
if ( ! self::is_wc_gte( '3.0' ) ) {
$options['featured_first'] = __( 'Featured First', 'woocommerce-extra-product-sorting-options' );
}
return $options;
}
foreach( $new_sorting_options as $option ) {
switch ( $option ) {
case 'by_stock':
$sortby['by_stock'] = __( 'Sort by availability', 'woocommerce-extra-product-sorting-options' );
break;
}
}
return $sortby;
}
public function add_new_shop_ordering_args( $sort_args ) {
// If we have the orderby via URL, let's pass it in.
// This means we're on a shop / archive, so if we don't have it, use the default.
if ( isset( $_GET['orderby'] ) ) {
$orderby_value = wc_clean( $_GET['orderby'] );
} else {
$orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
}
// Since a shortcode can be used on a non-WC page, we won't have $_GET['orderby'] --
// grab it from the passed in sorting args instead for non-WC pages.
// Don't use this on WC archives so we don't break the default option
if ( ! is_post_type_archive( 'product' ) && ! is_shop() && isset( $sort_args['orderby'] ) ) {
$orderby_value = $sort_args['orderby'];
}
$fallback = apply_filters( 'wc_extra_sorting_options_fallback', 'title', $orderby_value );
$fallback_order = apply_filters( 'wc_extra_sorting_options_fallback_order', 'ASC', $orderby_value );
switch( $orderby_value ) {
case 'by_stock':
$sort_args['orderby'] = array( 'meta_value_num' => 'DESC', $fallback => $fallback_order );
$sort_args['meta_key'] = '_stock';
break;
}
return $sort_args;
}
I have category page which is redirecing to archieve.php.
you can see here : https://www.dealfinder.lk/category/dining/
There are two sticky posts at the top.
1) Up to 25% OFF at &Co Pub and Kitchen with COMBANK Cards
2) 20% OFF at Robata – Movenpick Hotel Colombo for all HSBC Credit Cards
My pagination is 10 items per post
Right now, it shows me 12 items per post.
Here is my code :
function yell_category_sticky_posts( $posts, $wp_query ) {
global $wp_the_query;
// Don't continue if this isn't a category query, we're not in the main query or we're in the admin
if ( ! $wp_query->is_category || $wp_query !== $wp_the_query || is_admin() )
return $posts;
global $wpdb;
$q = $wp_query->query_vars;
$page = absint( $q['paged'] );
if ( empty( $page ) )
$page = 1;
$post_type = $q['post_type'];
$sticky_posts = get_option( 'sticky_posts' );
if ( $wp_query->is_category && $page <= 1 && is_array( $sticky_posts ) && !empty( $sticky_posts ) && ! $q['ignore_sticky_posts'] ) {
$num_posts = count( $posts );
$sticky_offset = 0;
// Loop over posts and relocate stickies to the front.
for ( $i = 0; $i < $num_posts; $i++ ) {
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 ) );
// Increment the sticky offset. The next sticky will be placed at this offset.
$sticky_offset++;
// Remove post from sticky posts array
$offset = array_search( $sticky_post->ID, $sticky_posts );
unset( $sticky_posts[$offset] );
}
}
// If any posts have been excluded specifically, Ignore those that are sticky.
if ( !empty( $sticky_posts ) && !empty( $q['post__not_in'] ) )
$sticky_posts = array_diff( $sticky_posts, $q['post__not_in'] );
// Fetch sticky posts that weren't in the query results
if ( !empty( $sticky_posts ) ) {
$stickies__in = implode( ',', array_map( 'absint', $sticky_posts ));
// honor post type(s) if not set to any
$stickies_where = '';
if ( 'any' != $post_type && '' != $post_type ) {
if ( is_array( $post_type ) )
$post_types = join( "', '", $post_type );
else
$post_types = $post_type;
$stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
}
$stickies = $wpdb->get_results( "SELECT wp_posts.* FROM $wpdb->posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (" . get_term( $wp_query->query_vars['cat'], 'category' )->term_taxonomy_id . ") ) AND $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
foreach ( $stickies as $sticky_post ) {
// Ignore sticky posts are not published.
if ( 'publish' != $sticky_post->post_status )
continue;
array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
$sticky_offset++;
}
}
}
return $posts;
}
add_filter( 'the_posts', 'yell_category_sticky_posts', 10, 2 );
My Issue:
I want to show 10 posts per page, currently it shows 12 posts
per page with sticky post.
This question is for master not for new learner.
Anybody master here? Thanks in advance
The below function pushes stickies to the top, you should be able to use this to help in your case.
add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
$stickies = array();
foreach($posts as $i => $post) {
if(is_sticky($post->ID)) {
$stickies[] = $post;
unset($posts[$i]);
}
}
return array_merge($stickies, $posts);
}
As I suggested in the comment, save the 'sticky posts' in meta (assuming is_featured_post as the 'meta key').
Run these only once to set the meta value for existing posts. You can skip this since you are already saving in the meta.
// set meta value of all posts to 0
$all_posts = get_posts(array('post_type'=>'post','posts_per_page'=>-1));
if( is_array( $all_posts ) )
{
foreach( $all_posts as $post ) {
update_post_meta( $post->ID, 'is_featured_post', '0' );
}
}
// set meta value of all sticky posts alone to 1
$sticky_posts = get_option( 'sticky_posts' );
if( is_array( $sticky_posts ) )
{
foreach ( $sticky_posts as $sticky_post ) {
update_post_meta( $sticky_post, 'is_featured_post', '1' );
}
}
The below function will update the new sticky meta is_featured_post each time a post updated (or new post saved).
function save_sticky_meta( $post_id ) {
if ( isset( $_REQUEST['sticky'] ) ) {
update_post_meta( $post_id, 'is_featured_post', '1' );
}
else {
update_post_meta( $post_id, 'is_featured_post', '0' );
}
}
add_action( 'save_post', 'save_sticky_meta' );
add_action( 'edit_post', 'save_sticky_meta' );
Then use pre_get_posts action to set the category query. We are ordering by both 'meta' and 'date' descending to show the latest at top.
function include_sticky_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_category() ) {
$query->set( 'meta_key', 'is_featured_post' );
$query->set( 'sticky_sort', true ); //custom sticky order query
$query->set( 'orderby', 'meta_value_num date' );
$query->set( 'order', 'DESC' );
}
}
add_action( 'pre_get_posts', 'include_sticky_posts' );
If you want to randomize non-sticky posts, change the order using the_posts filter as below.
add_filter( 'the_posts', 'sticky_posts_sort', 10, 2 );
function sticky_posts_sort( $posts, $query )
{
// if custom sort set from category query
if ( true !== $query->get( 'sticky_sort' ) )
return $posts;
// loop through posts & save sticky & other posts in seperate arrays
$sticky_posts = get_option( 'sticky_posts' );
$sticky_array = array();
$posts_array = array();
foreach ( $posts as $p ) {
if( in_array( $p->ID, $sticky_posts ) )
$sticky_array[] = $p;
else
$posts_array[] = $p;
}
// merge both arrays and randomize non-sticky posts alone
if( is_array( $posts_array ) )
shuffle( $posts_array );
if( is_array( $sticky_array ) && is_array( $posts_array ) )
$posts = array_merge( $sticky_array, $posts_array );
elseif( is_array( $sticky_array ) )
$posts = $sticky_array;
else
$posts = $posts_array;
return $posts;
}