Skip to content

Instantly share code, notes, and snippets.

View steve-brett's full-sized avatar

Steve Brett steve-brett

View GitHub Profile
@steve-brett
steve-brett / edit_attachment_meta.php
Last active August 10, 2020 16:48
Allow users who don't have edit_posts capability to edit attachment metadata
<?php
/**
* Allow users who don't have edit_posts capability to edit attachment metadata.
* From @grapplerulrich on https://core.trac.wordpress.org/ticket/19834#comment:42
*/
add_filter( 'register_post_type_args', function( $args, $post_type ) {
if ( 'attachment' === $post_type ) {
$args['capabilities']['edit_posts'] = 'upload_files';
$args['capabilities']['upload_files'] = 'upload_files';
}
@steve-brett
steve-brett / query_events_in_period.php
Created June 15, 2020 10:21
Display all events in a given period
<?php
$start_of_week = date( 'Y-m-d', strtotime( 'monday this week' ) );
$end_of_week = date( 'Y-m-d', strtotime( 'sunday this week' ) );
$event_args['meta_query'] = array(
array(
'relation' => 'AND',
// Event starts before end of week, and finishes after start of week.
// This covers all instances of events overlapping the week period.
array(
@steve-brett
steve-brett / filter_cpt_by_tag.php
Last active May 22, 2020 15:46
Filter custom post type by tag
<?php
/**
* Adds admin area filtering for custom post type tags.
*
* This is separate because, for some reason, the taxonomy
* is 'post_tag' but the query var is 'tag'.
*
* @param string $post_type The post type slug.
* @return void
*/
@steve-brett
steve-brett / filter_cpt_by_taxonomies.php
Last active May 22, 2020 15:45
Filter custom post type by taxonomy
<?php
/**
* Adds admin area filtering for CPT taxonomies.
*
* @param string $post_type The post type slug.
* @return void
*/
function filter_cpt_by_taxonomies( $post_type ) {
// Apply this only on a specific post type