Get posts in custom post type categories

You can use any of Category Parameters for your WP_Query cat (int) – use category id. category_name (string) – use category slug. category__and (array) – use category id. category__in (array) – use category id. category__not_in (array) – use category id. //list all posts by category $args = array( ‘post_type’ => ‘works’, // NOT ‘Works’ // … Read more

To display the category of a specific custom post type

For a custom post type, use get_the_terms() instead: <?php $post_terms = get_the_terms( get_the_ID(), ‘your-taxonomy’ ); ?> <p> Category: <?php echo $post_terms[0]->name; ?></p> Much like the results from get_the_category, the returned value of get_the_terms() is an array of term objects for the taxonomy that you specify when calling the function. In the example code, that is … Read more

How to fix 404 on post after prepending Custom Post Type url with Custom Taxonomy Term slug

First, you don’t need to have two actions called during init, they can both be done under the same action function — so move the register CPT and taxonomy to a single function (or call separate functions from the main one) — no need to have two init actions. You should also be registering the … Read more

Mandatory excerpt for custom post type

The code adds a filter to wp_insert_post_data: add_filter(‘wp_insert_post_data’, ‘mandatory_excerpt’); And here’s the callback: function mandatory_excerpt($data) { $excerpt = $data[‘post_excerpt’]; if (empty($excerpt)) { if ($data[‘post_status’] === ‘publish’) { add_filter(‘redirect_post_location’, ‘excerpt_error_message_redirect’, ’99’); } $data[‘post_status’] = ‘draft’; } return $data; } The filter callback is passed $data, which as per the Codex includes the following post data: ‘post_author’, … Read more

add_rewrite_rule wordpress ignoring url pattern, wp rewrite not working

You are trying to match 7-number-cities-starting-with-pa URL to ^([^/]*)-letter-words-starting-with- pattern in second rule. These rules should work: add_action( ‘init’ , ‘se336474_rewrite_rules’, 5 ); add_filter( ‘query_vars’, ‘se336474_query_vars’ ); function se336474_rewrite_rules() { // single pattern for URLs: // site.com/cities-starting-with-pa/ => start_letter=”pa” // site.com/7-cities-starting-with-pi/ => start_letter=”pi”, length=7 add_rewrite_rule( ‘^(?:([0-9]+)-)?cities-starting-with-([^/]*)/?$’, ‘index.php?page_id=340&start_letter=$matches[2]&length=$matches[1]’, ‘top’ ); // // OR for URLs: // … Read more