Skip to content

Instantly share code, notes, and snippets.

View koskinenaa's full-sized avatar

Antti Koskinen koskinenaa

View GitHub Profile
@koskinenaa
koskinenaa / Disable WordPress RSS feed date ordering
Last active May 8, 2023 14:17
RSS feeds read by WordPress are ordered by date by default. Use `wp_feed_options` action to disable date ordering and consume the items in their own order.
<?php
// E.g. fetch_feed('https://www.example.com/some-feed');
add_action( 'wp_feed_options', 'my_feed_options', 10, 2 );
function my_feed_options( SimplePie $feed, $url ): void {
$my_feed = 'https://www.example.com/some-feed';
if ( ! is_string($url) || $my_feed !== $url ) {
return;
@koskinenaa
koskinenaa / wordpress-filter-custom-post-type-by-custom-taxonomy.php
Last active June 6, 2020 21:36
Add taxonomy to a WordPress post type as a filter. See https://gist.github.com/koskinenaa/86fd3acd75959e9aa0635ce00934e26c for multiple post types and taxonomies
public function filter_post_type_by_taxonomy( $post_type, $which ) {
if ( 'my_post_type' == $post_type ) {
$taxonomy = get_taxonomy( 'my_taxonomy' );
if ( $taxonomy ) {
// Retrieve taxonomy terms
$terms = get_terms( $taxonomy->name );
// Display filter HTML
echo "<select name='{$taxonomy->name}' id='{$taxonomy->name}' class='postform'>";
echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'text-domain' ), $taxonomy->labels->name ) . '</option>';
@koskinenaa
koskinenaa / wordpress-filter-custom-post-types-by-custom-taxonomies.php
Last active November 16, 2020 12:15
Add filtering by custom taxonomies to a post type. Note when registering the custom taxonomies 'query_var' should not be set to false. If so, this function won't work,
/**
* Add taxonomy filtering to custom post types
*
* Original example for one post type, https://generatewp.com/filtering-posts-by-taxonomies-in-the-dashboard/
*
*/
function filter_cpt_by_taxonomies( $post_type, $which ) {
// Affected post types
$post_types = array(
'my_post_type',
@koskinenaa
koskinenaa / wp-query-posts-into-columns.php
Last active February 28, 2020 16:09
Break WP_Query posts into separate columns
<?php
// Custom WP_Query
$args = array();
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) :
// Number of columns
$col_count = 3;
// Get post count
$post_count = $my_query->found_posts;