Skip to content

Instantly share code, notes, and snippets.

View someguy9's full-sized avatar
🏠
Working from home

Andy Feliciotti someguy9

🏠
Working from home
View GitHub Profile
<?php
$meta_query_args = array(
'meta_query' => array(
array(
'key' => 'my_date_field',
'value' => array( strtotime('2022-01-01'), strtotime('2022-12-31') ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
@someguy9
someguy9 / wordpress-query-posts-between-dates.php
Created October 28, 2022 15:15
Using meta_query to query posts between dates in WordPress
<?php
$meta_query_args = array(
'meta_query' => array(
array(
'key' => 'my_date_field',
'value' => array( '2022-01-01', '2022-12-31' ),
'type' => 'date',
'compare' => 'BETWEEN'
)
)
@someguy9
someguy9 / wordpress-display-title.php
Last active October 25, 2022 15:34
WordPress get current page title
<?php
// Display post title
echo get_the_title();
@someguy9
someguy9 / increase-wordpress-auto-scaling.php
Last active October 25, 2022 15:13
Increase the image threshold for WordPress auto resize feature
<?php
// Increase the image resize threshold to 4000px on the longest edge
function smartwp_big_image_size_threshold( $threshold ) {
return 4000;
}
add_filter( 'big_image_size_threshold', 'smartwp_big_image_size_threshold', 999, 1);
@someguy9
someguy9 / disable-wordpress-auto-scaling.php
Last active October 25, 2022 14:56
Disable image scaling in WordPress (Introduced in WordPress 5.3)
<?php
// Disable WordPress' automatic image scaling feature
add_filter( 'big_image_size_threshold', '__return_false' );
@someguy9
someguy9 / display-current-post-author-avatar.php
Created August 4, 2022 14:45
Show the author avatar of the current WordPress post.
<?php
// Display current post's author avatar (includes <img> tag)
echo get_avatar( get_the_author_meta( 'ID' ), 96 );
// Display current post's author avatar URL
echo get_avatar_url( get_the_author_meta( 'ID' ), array( 'size' => 96 ) );
@someguy9
someguy9 / wordpress-get-current-user-logged-in-avatar.php
Created August 4, 2022 14:25
Get the current avatar for logged in user in WordPress
<?php
// Ensure user is logged in
if( is_user_logged_in() ) {
// Display current logged in user's avatar (includes <img> tag)
echo get_avatar( get_current_user_id(), 96 );
// Display current logged in user's avatar URL
echo get_avatar_url( get_current_user_id(), array( 'size' => 96 ) );
}
<?php
// Display post title using post ID
echo get_the_title( 5 );
// Display post title and safely escape value
echo esc_html( get_the_title() );
// Using the_title function to automatically echo the title, while you can append or prepend the title using the function as displayed below
the_title( 'Before:', ' - After' )
@someguy9
someguy9 / php-current-page-url-with-query-string.php
Created July 11, 2022 12:51
Get current page URL with query string in PHP
<?php
$url_parts = parse_url( home_url() );
$current_url_with_query_string = $url_parts['scheme'] . "://" . $url_parts['host'] . add_query_arg( NULL, NULL );
echo $current_url_with_query_string;
@someguy9
someguy9 / wordpress-get-current-slug.php
Created July 11, 2022 12:40
Get the current slug in WordPress.
<?php
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
echo $current_slug;