Create side bar widget showing list of years as hyperlinks for a custom post type

First, create the custom WordPress widget. This will query all the years that have at least one post for your custom post type “Meeting.” Save the following code in your theme’s functions.php file: class Meeting_Archive_Widget extends WP_Widget { public function __construct() { parent::__construct(‘meeting_archive’, ‘Meeting Archive’, array(‘description’ => ‘List Meetings by Year’)); } public function widget($args, … Read more

Show only taxonomy types terms associated with a custom post type in WordPress PHP

UPDATE The custom SQL query below can be used to produce a list of unique types taxonomy terms that have been assigned to newsroom posts. In PHP, you can call the function get_newsroom_types_terms to get the list: $terms = get_newsroom_types_terms();. Custom SQL query function get_newsroom_types_terms() { global $wpdb; $table_prefix = $wpdb->prefix; $taxonomy = ‘types’; $post_type=”newsroom”; … Read more

How to work with URLs where sometimes a post or a subcategory is in the same part of the URL structure

Your challenge lies in the way WordPress interprets URL patterns and resolves them to query variables. When you have a URL structure where the third segment can be either a subcategory or a product name, WordPress struggles to differentiate between the two. This is a common issue in WordPress when using custom post types and … Read more

Dynamic WordPress rewrite rules for multiple custom post types

Jacob’s comment got me on the right track. Here’s what worked for my issue: add_action(‘init’, ‘cpt_rewrite’); function cpt_rewrite(){ $args = array( ‘public’ => true, ‘_builtin’ => false, ); $post_types = get_post_types( $args ); if ( $post_types ) { foreach ( $post_types as $post_type ) { add_rewrite_rule(‘^’.$post_type.’/([0-9]{4})/([0-9]{2})/?’,’index.php?post_type=”.$post_type.”&year=$matches[1]&monthnum=$matches[2]’,’top’); add_rewrite_rule(‘^’.$post_type.’/([0-9]{4})/?’,’index.php?post_type=”.$post_type.”&year=$matches[1]’,’top’); } } } I used get_post_types() to get … Read more

Archive pagination – second page shows exactly the same posts

Put this code <div class=”row”> <div class=”col-md-8 col-md-offset-2″> <?php $args = array( ‘posts_per_page’ => 3, ‘post_type’ => ‘work’, ‘paged’ => get_query_var(‘paged’) ? get_query_var(‘paged’) : 1 ); $myposts = new WP_Query($args); ?> <?php single_term_title(); ?> <?php if ( $myposts->have_posts() ) : ?> <?php while ( $myposts->have_posts() ) : $myposts->the_post(); ?> <?php get_template_part( ‘content’, get_post_format() );?> <?php … Read more