I'm using WP Grid Builder and I need to order my grid with two fields:
DATE (DESC) and PARENT (ASC).
So that I have the most recent posts AND the parent posts to show first.
That way, I push the child posts to the bottom
Is that possible?
For your reference: https://docs.wpgridbuilder.com/resources/filter-grid-query-args/
This is what I came up with, but it's not working:
<?php
function grid_query_sorting_date_parent( $query_args, $grid_id ) {
global $post;
// If it matches grid id 1.
if ( 1 === $grid_id ) {
$query_args['orderby'] = array('date' => 'DESC' , 'parent' => 'ASC');
}
return $query_args;
}
add_filter( 'wp_grid_builder/grid/query_args', 'grid_query_sorting_date_parent', 10, 2 );
Related
Update! Read to the bottom for solve!
/*remove Edit Trash, and quickedit of pages with specific metavalues for certain roles
*/
add_filter( 'page_row_actions', 'remove_page_row_actions', 10, 2 ); // 2, not 1
// pass $post object as second argument.
function remove_page_row_actions( $actions, $post ) {
global $current_user;
get_currentuserinfo();
//specify roles except administrator (but you need to add child roles based off administrator ex. adminsub)
if(
( (!current_user_can('administrator') || current_user_can('adminsub') )
||
// specify a list of role(s)
( current_user_can('adminsub') || current_user_can('author') )
) )
{
//search which type of $post
$post_types_affected = array('page');
//Dynamically get 1 post ID's in an array for a post that matches multiple values (im repeating 1 pointlessly)
//$array_of_values_to_match = array( '1', '1', '1' );
$array_of_values_to_match = array( 1 );
$metakey_to_match = 'checkboxmeta1';
// set some initial args
$args = array(
'post_type' => 'page',
'meta_query' => array(),
);
// if there's more than 1 value, add the relation arg
if( 1 < count( $array_of_values_to_match ) ){
$args['meta_query']['relation'] = 'AND';
}
// for each of the array values, add a meta query for that value
foreach( $array_of_values_to_match as $val ){
$args['meta_query'][] = array(
'key' => $metakey_to_match,
'value' => $val,
'compare' => '='
);
}
$postlistarray = get_posts( $args );
//end of function which an array or post arrays which each contain a post ID and a metakey of 1
//must create an array column, becuase that is what $x = array(111,212) does
$post_ids_affected = array_column($postlistarray, 'ID');
//check array_column for post IDs, and remove functionality below
if ( in_array( $post->post_type, $post_types_affected )
&& in_array( $post->ID, $post_ids_affected ) )
{
unset( $actions['inline hide-if-no-js'] );
unset( $actions['edit'] );
unset( $actions['trash'] );
unset( $actions['pa_duplicator'] );
}
}
return $actions;
}
$post_ids_affected ends up being something like
array(0=>1000 , 1=> 1023, ... 4 => 524)
but it only goes up to 4, when there are like 10 pages atleast that have metabox value of 1. and the post ID list is from most recently created to oldest... but 0 to 4 is only 5 pages...not sure why I cant pull more into my array?
chris Haas was able to see the issue so I changed
//set some initial args
$args = array(
'post_type' => 'page',
'meta_query' => array(),
);
to this instead
$args = array(
'post_type' => get_post_types(),
'meta_query' => array(),
'numberposts' => -1,
);
and it works! My next concern however is trying to get this to apply also to posts and to custom posts types.. since it only affects pages right now?
SOLVED:
I just made the above adjustment
and the 2 following
changed
//search which type of $post
$post_types_affected = array('page');
to
//search which type of $post
$post_types_affected = array('page' , 'post');
and the duplicated the filter for posts!
/*remove Edit Trash, and quickedit of pages with specific metavalues for certain roles
*/
add_filter( 'page_row_actions', 'remove_page_row_actions', 10, 2 ); // 2, not 1
// pass $post object as second argument.
function remove_page_row_actions( $actions, $post ) {
to this
/*remove Edit Trash, and quickedit of pages with specific metavalues for certain roles
*/
add_filter( 'page_row_actions', 'remove_page_row_actions', 10, 2 ); // 2, not 1
add_filter( 'post_row_actions', 'remove_page_row_actions', 10, 2 ); // 2, not 1
// pass $post object as second argument.
function remove_page_row_actions( $actions, $post ) {
I have split up my code into part 1 , part 2, and part 3
part 1 and 2 are working just fine... they are checking post types and scanning for metadata
Its only Part 3 that I am completely missing the bill
/*remove Edit Trash, and quickedit of pages with specific metavalues for certain roles
*/
add_filter( 'page_row_actions', 'remove_page_row_actions', 10, 2 ); // 2, not 1
add_filter( 'post_row_actions', 'remove_page_row_actions', 10, 2 ); // 2, not 1
// pass $post object as second argument.
function remove_page_row_actions( $actions, $post ) {
//PART 1 SPECIFY USERS
global $current_user;
get_currentuserinfo();
if(
(
//specify roles except administrator (but you need to add child roles based off administrator ex. adminsub)
(
!current_user_can('administrator')
||
current_user_can('adminsub')
)
||
// specify a list of role(s)
(
current_user_can('adminsub')
||
current_user_can('author')
)
)
)
{
// PART 2 SEARCH WHICH TYPES OF POSTS TO GET A SPECIFIC METADATA FROM
$post_types_affected = array('page' , 'post'); // I ADDED POST HERE STILL WORKS
// print_r($post_types_affected); // use this to see which posts are affected
//only get post IDs for a metavalue which has ALL of the following EXACT values (I changed it to just 1)
//$array_of_values_to_match = array( '1', '1', '1' );
$array_of_values_to_match = array( 1 );
$metakey_to_match = 'checkboxmeta1';
$args = array(
'post_type' => get_post_types(),
'meta_query' => array(),
'numberposts' => -1,
);
// if there's more than 1 value, add the relation arg
if( 1 < count( $array_of_values_to_match ) ){
$args['meta_query']['relation'] = 'AND';
}
// for each of the array values, add a meta query for that value
foreach( $array_of_values_to_match as $val ){
$args['meta_query'][] = array(
'key' => $metakey_to_match,
'value' => $val,
'compare' => '='
);
}
$postlistarray = get_posts( $args );
//end of function which an array or post arrays which each contain a post ID and a metakey of 1
//must create an array column, becuase that is what $x = array(111,212) does
$post_ids_affected = array_column($postlistarray, 'ID');
//PART 3 check array_column for post IDs, and remove functionality below from posts which had the metadata
if ( in_array( $post->post_type, $post_types_affected )
&& in_array( $post->ID, $post_ids_affected ) ) {
unset( $actions['inline hide-if-no-js'] );
unset( $actions['edit'] );
unset( $actions['trash'] );
unset( $actions['pa_duplicator'] );
}
}
return $actions;
}
what I am trying to do is for these roles from 1 when interacting with posts from 2, I want to remove ALL EDIT and TRASH capabilities.
so obviously right now ive just removed some actions lol such as edit and quickedit.. but I just realized the user can just click on the page edit.php link, or even just change the url to soemthing like
https://mywebsite/wp-admin/post.php?post=2752&action=edit
or
remotely trash or edit it some other way through URL or other method..
how do I just completely remove the edit and trash capability of that role for part 3, so that its view only? and maybe view and comment moderation?
I hope I didnt waste alot of time by trying to hook into page_row_actions and post_row_actions lol... maybe I need to be putting this code into like admin_init or something but I am sort of a noob lol
after about 8 hours.. I realized a plugin call advanced access manager, which is free, has 2 toggle checkboxes on each page for this exactly lol.
why reinvent the wheel I Guess
In WooCommerce using wc_get_products() function I would like to find a way to filter products by name using the LIKE operator.
Actually I am only able to filter products by a specific defined name with:
$args = array(
'limit' => 5,
'name' => 'Test',
);
$result = wc_get_products( $args );
Is it possible to filter products where the name is LIKE 'test'?
If you look to WooCommerce official documentation for WC_Product_Query at the end on the section "Adding Custom Parameter Support" you will see that you can manipulate the WC_Product_Query with a custom hooked function.
So to filter the query with a product name "LIKE" parameter, you can extend the query with the search "s" argument, that will do the trick as follow:
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );
function handle_custom_query_var( $query, $query_vars ) {
if ( isset( $query_vars['like_name'] ) && ! empty( $query_vars['like_name'] ) ) {
$query['s'] = esc_attr( $query_vars['like_name'] );
}
return $query;
}
Code goes in functions.php file of your active child theme (or active theme). tested and works.
USAGE example with custom argument "like_name":
$args = array(
'limit' => 5,
'like_name' => 'test',
);
wc_get_products( $args );
I am using Beaver Builder with WordPress and am using Advanced Posts to display my custom post types. I have 6 teams and about 30 players that belong to each team. I want to display the team's roster on the team page but can't filter by team when selecting players to show.
I found this method: https://www.ultimatebeaver.com/docs/filter-query-parameters-advanced-posts/
But I am having trouble comparing the meta keys between players and teams and don't know where to begin.
This is what I have so far:
global $post;
$current_team = $post->post_name;
if ( $settings->id == 'team-roster' ) {
$args['meta_key'] = 'team';
$args['posts_per_page'] = '30';
$args['meta_query'] =
array(
'key' => 'team',
'value' => '$current_team',
'compare' => '=',
);
}
return $args;
I am trying to compare to the page slug, which is the same as the values stored in the meta_key.
I would like each team page to show the players on the team as well as use this same module on the players page to show the rest of the players. Open to other ideas as well if this isn't the right method.
Ended up using categories to filter instead of the relationship meta_key field. Found out that the relationship field is stored as an array so it wasn't comparing correctly. Here's my solution:
global $post;
$current_team = $post->post_name;
if ( $settings->id == 'team-roster' ) {
$args['posts_per_page'] = '50';
$args['tax_query'] =
array(
array(
'taxonomy' => 'category',
'terms' => $current_team,
'field' => 'slug',
)
);
}
return $args;
I am trying to hide a product category from my product category menu.
add_filter( 'woocommerce_product_categories_widget_args', __NAMESPACE__ . '\\rv_exclude_wc_widget_categories' );
function rv_exclude_wc_widget_categories( $cat_args ) {
$cat_args['exclude'] = array('129'); // Insert the product category IDs you wish to exclude
$includes = explode(',', $cat_args['include']);
$cat_args['include'] = array_diff($includes,$cat_args['exclude']);
return $cat_args;
}
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-1, -129' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
The top function of my code successfully hides the category from my menu when actually in a category. However, it does not hide it from the main shop page. This is what I'm attempting with the bottom code, however, this doesn't appear to do anything.
Any ideas on how this can be done? The code is placed in my functions.php file.
EDIT: Trying to clarify what I'm asking.
When first opening my product page I now have the category 'TEST' hidden from my menu like below.
However, when I click into a product or category the menu goes back to displaying like below.
For your first function (hiding a specific product category from widget):
add_filter( 'woocommerce_product_categories_widget_args', 'exclude_product_categories_widget', 10, 1 );
function exclude_product_categories_widget( $list_args ) {
$categories = array('129');
if(isset( $list_args['include'])):
$included_ids = explode( ',', $list_args['include'] );
$included_ids = array_unique( $included_ids );
$included_ids = array_diff ( $included_ids, $categories );
$list_args['include'] = implode( ',', $included_ids);
else:
$list_args['exclude'] = $categories;
endif;
return $list_args;
}
To exclude your products under product category (term ID) 129 in shop and archive pages use the following dedicated Woocommerce hooked function:
add_filter('woocommerce_product_query_tax_query', 'exclude_product_category_in_tax_query', 10, 2 );
function exclude_product_category_in_tax_query( $tax_query, $query ) {
if( is_admin() ) return $tax_query;
// HERE Define your product categories Terms IDs to be excluded
$terms = array( 129 ); // Term IDs
// The taxonomy for Product Categories custom taxonomy
$taxonomy = 'product_cat';
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'term_id', // Or 'slug' or 'name'
'terms' => $terms,
'operator' => 'NOT IN', // Excluded
'include_children' => true // (default is true)
);
return $tax_query;
}
Code goes in function.php file of your active child theme (or theme). Tested and works.