Skip to content

Instantly share code, notes, and snippets.

function get_cat( $_slug ) {
$_categories = get_terms( 'category', array( 'hide_empty' => false, 'order' => 'ASC' ) );
$_result = array_values( array_filter( $_categories, function($obj){
if (isset($obj->slug)) {
if( $obj->slug == [needle] ):
$_term_id = $obj->term_id;
return true;
endif;
@Robzelf
Robzelf / paginate_through_array
Created July 30, 2020 21:27
Paginate through array
page_through_array: function( page_size, page_number ) {
return = _array.slice( ( page_number - 1 ) * page_size, page_number * page_size );
}
@Robzelf
Robzelf / Wordpress: Filter by custom taxonomy & custom field
Last active October 9, 2019 09:54
Wordpress: Filter by custom taxonomy & custom field
$_args = array (
'post_type' => %post_type%,
'tax_query' => array (
array ( 'taxonomy' => %tax_name%, 'field'=> 'term_id', 'terms' => %term_id% ),
),
'meta_query' => array (
@Robzelf
Robzelf / Wordpress: Default agenda sorting
Last active August 26, 2021 21:05
Sort entries by date and skip everything before today.
function update_agenda_archive($query) {
if ( !is_admin() && $query->is_post_type_archive('agenda') ) {
$_today = date('Ymd');
$query->set('meta_key', 'event__start_date');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'ASC');
$query->set('meta_query', array(
array(
@Robzelf
Robzelf / Wordpress: sort query by multiple keys
Last active March 16, 2017 13:06
Sort query by multiple keys
function update_archive( $query ) {
$query->set('meta_query' , array(
'day_clause' => array( 'key' => 'event__day' ),
'time_clause' => array( 'key' => 'event__start_time' )));
$query->set('orderby' , array( 'day_clause' => 'ASC', 'time_clause' => 'ASC' ));
}
add_action( 'pre_get_posts', 'update_archive' );