WordPress Template Hierarchy

You can use the dynamic filter {type}_template (where {type} is the current query type e.g. single, index etc.) found in get_query_template(): function wpse_204848_get_id_template( $template ) { if ( $post = get_queried_object() ) { if ( $_template = locate_template( “single-{$post->post_type}-{$post->ID}.php” ) ) $template = $_template; } return $template; } add_filter( ‘single_template’, ‘wpse_204848_get_id_template’ ); For a portfolio … Read more

How loop through posts based on custom fields

To select posts that have a meta value, use the EXISTS comparison operator. $args = array( ‘post_type’ => ‘jobs’, ‘posts_per_page’ => 12, ‘paged’=> $paged, ‘tax_query’ => array( array( ‘taxonomy’ => ‘job_category’, ‘field’ => ‘slug’, ‘terms’ => $term->slug, ) ), ‘meta_query’ => array( array( ‘key’ => ‘_shop’, ‘compare’ => ‘EXISTS’ ) ) );

Remove header and footer but KEEP all css and js etc for a custom post template?

If someone needs something similar, I actually found an answer to my issue and it is to pass arguments to get_header() and get_footer(). <?php get_header(‘customposttype); ?> <div style=”width:500px;”> <?php $id = get_the_id(); echo do_shortcode( ‘[skwidget id=”‘ . $id . ‘”]’ ); ?> </div> <?php get_footer(‘customposttype’); ?> and copy header.php to header-{customposttype}.php and footer.php to footer-{customposttype}.php … Read more

Custom role capabilities to administrator not taking effect (no plugin)

Capabilities are stored in database, that is why they keep active even if you remove your function. Capabilities need to be expressly removed: $admin = get_role(‘author’); $admin->remove_cap( ‘edit_teacher’ ); And, because they are stored in database, you should stop adding them in every page load (as you are doing in init event). Usually, you should … Read more

Is there a way to list posts of only a certain category

You can filter the posts list by appending ?category_name=xx to the admin posts list URL, and you can add a submenu page with that URL as the target via add_submenu_page: add_action( ‘admin_menu’, ‘wpd_admin_menu_item’ ); function wpd_admin_menu_item(){ add_submenu_page( ‘edit.php’, ‘Page title’, ‘Menu item title’, ‘edit_posts’, ‘edit.php?category_name=somecat’ ); }

Custom taxonomy template not working with simple loop. Multiple CPT using the same taxonomy

WP defaults to showing normal native Posts in archives. It won’t automagically pick up which post types you want in your archive. You will have to adjust main query for it to explain that to it, with something like: add_action( ‘pre_get_posts’, function ( WP_Query $query ) { if ( $query->is_main_query() && $query->is_tax( ‘department’ ) ) … Read more

Turn query string to sub page

Put this code in your functions.php– function the_dramatist_url_rewrite() { global $wp_rewrite; add_rewrite_tag(‘%sub%’, ‘([^&]+)’); add_rewrite_rule( ‘^events/%year%/%monthnum%/%day%/%postname%/?’, ‘index.php/events/%year%/%monthnum%/%day%/%postname%?sub=$matches[1]’, ‘top’ ); } add_action(‘init’, ‘the_dramatist_url_rewrite’, 10, 0); The go to the permalinks page in the settings menu at dashboard and hit Save Changes button. Hope this is gonna help.

How to: add option to add more checkboxes on custom post type

Your question is very abstract. As far I understood you want some thing like categories. So for that adding metaboxes is not necessary. I think categories are enough. If categories are not full filling your need then make another custom taxonomy with ‘hierarchical’ => true,. I think it will serve your need. Also giving permissions … Read more

Append taxonomy terms as class names in markup?

Differ the output until you have a collection ready. $terms = get_the_terms( $post->ID, ‘teams’ ); // create a collection with your default item $classes = array(‘person’); foreach ($terms as $term) { // add items $classes[] = $term->slug; } // output all the classes together with a space to separate them. echo ‘<div class=”‘ . implode(‘ … Read more