Guest User

Untitled

a guest
Nov 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Have WordPress match postname to any of our public post types (post, page, race).
  5. * All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts.
  6. * By default, WordPress only accounts for posts and pages where the slug is /post-name/.
  7. *
  8. * @param $query The current query.
  9. */
  10. function gp_add_cpt_post_names_to_main_query( $query ) {
  11.  
  12. // Bail if this is not the main query.
  13. if ( ! $query->is_main_query() ) {
  14. return;
  15. }
  16.  
  17. // Bail if this query doesn't match our very specific rewrite rule.
  18. if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
  19. return;
  20. }
  21.  
  22. // Bail if we're not querying based on the post name.
  23. if ( empty( $query->query['name'] ) ) {
  24. return;
  25. }
  26.  
  27. // Add CPT to the list of post types WP will include when it queries based on the post name.
  28. $query->set( 'post_type', array( 'post', 'page', 'race' ) );
  29. }
  30. add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );
Add Comment
Please, Sign In to add comment