Skip to content

Instantly share code, notes, and snippets.

View imagebox's full-sized avatar

Imagebox imagebox

View GitHub Profile
@imagebox
imagebox / query-posts-sort-by-acf-year.php
Last active May 21, 2022 07:11
[WordPress | Query Posts, Show Upcoming Events Posts By ACF Date] #WordPress #ACF
// not for the Events Calendar posts but for an Events CPT or sommething similar that uses ACF fields for dates.
// example from https://idelic.com/resource-type/events/
<?php
$today = current_time('Ymd');
$post_query_args = array(
'post_type' => 'resources',
'posts_per_page' => -1,
@imagebox
imagebox / wp-filter-tax-by-terms.php
Last active February 12, 2021 13:18
[WordPress Dashboard | Filter By Taxonomy Terms] #WordPress #taxonomy #terms #dashboard #filter
// example from https://airetechnologies.com/, used here on resources CPT to filter by taxonomy resourctype
function filter_cars_by_taxonomies( $post_type, $which ) {
// Apply this only on a specific post type
if ( 'resources' !== $post_type )
return;
// A list of taxonomy slugs to filter by
$taxonomies = array( 'resourcetype' );
@imagebox
imagebox / query-post-type-group-by-year.php
Created February 11, 2021 20:07
[WordPress | Query Post Type, Group By Year] #WordPress #CPT
// see example https://www.carnegiehero.org/heroes/latest-award-announcements/press-releases/
<?php
$oldest = get_posts( 'post_type=post&post_status=publish&posts_per_page=1&order=ASC' );
$oldest_date = $oldest[0]->post_date;
$first_date = date('Y', strtotime($oldest_date));
$todays_date = date('Y');
$year_range = range($todays_date, $first_date);
@imagebox
imagebox / responsive-svg-with-js.js
Created February 11, 2021 19:55
[Responsive SVG with JS] #svg #responsive #js
// enclose svg in div with class svg-container
// in example below, 599/859 is the svg's height/width
$(window).on("resize", function () {
var myWidth = $('.svg-container').width();
var myHeight = (myWidth * 599/859);
$('.svg-container svg').css('width', myWidth + 'px');
$('.svg-container svg').css('height', myHeight + 'px');
}).resize();
@imagebox
imagebox / factwp-woocommerce-product-cat-query.php
Created November 21, 2019 20:34
[facetwp woocommerce product category query]
<div class="l-main-col">
<table class="parts-table">
<tbody class="facetwp-template">
<?php
$termid = get_queried_object()->term_id;
$args = array(
'post_type' => 'product',
'posts_per_page' => 18,
'facetwp' => true,
'paged' => $paged,
@imagebox
imagebox / filter-post-by-taxonomies-in-dashboard.php
Created June 20, 2019 15:53
[Filtering Posts by Taxonomies in the Dashboard] #cpt #wordpress #taxonomy
<?php
// https://generatewp.com/filtering-posts-by-taxonomies-in-the-dashboard/
// Allow sorting by taxonomies in admin
function filter_officers_by_taxonomies( $post_type, $which ) {
// Apply this only on a specific post type
if ( 'team' !== $post_type )
return;
@imagebox
imagebox / site-branding.php
Created April 25, 2019 19:09
[Image Logo]
<div class="site-branding">
<a href="<?php echo esc_url( home_url( '/' )); ?>" rel="home">
<img class="site-logo"
src="<?php bloginfo('template_directory'); ?>/assets/img/dist/branding/site-logo.png"
srcset="<?php bloginfo('template_directory'); ?>/assets/img/dist/branding/site-logo.png 1x, <?php bloginfo('template_directory'); ?>/assets/img/dist/branding/[email protected] 2x"
width="289"
height="67"
alt="<?php bloginfo( 'name' ); ?>">
</a>
</div>
@imagebox
imagebox / wp-query-tax-term.php
Created April 5, 2019 17:43
[WordPress | Query Posts by Taxonomy Term] WordPress | Query for posts by taxonomy term. #WordPress #query #taxonomy #term
<?php
$the_query = new WP_Query( array(
'post_type' => 'post_type_name',
'tax_query' => array(
array (
'taxonomy' => 'taxonomy_name',
'field' => 'slug',
'terms' => 'taxonomy_term',
)
@imagebox
imagebox / get-the-category.php
Created April 5, 2019 14:48
[WordPress | Get the First Category] // Get the first category on a single post by slug or name. #WordPress #post #category
<?php
// Get the first category on a single post by slug or name.
$category = get_the_category();
$currentcat = $category[0]->cat_ID;
$currentcatname = $category[0]->cat_name;
$currentcatslug = $category[0]->slug;
echo $currentcatname;
echo $currentcatslug;
@imagebox
imagebox / get-first-term.php
Created April 5, 2019 14:36
[WordPress | Get the First Term] Get the first term on a single post by slug or name. #WordPress #cpt #taxonomy #term
<?php
// Get the first term on a single post by slug or name.
$tax = 'taxonomy_name';
$terms = get_the_terms( get_the_ID(), $tax );
$term_slug = array();
$term_name = array();
if ( $terms && ! is_wp_error( $terms )) {