1

That is ancient issue, but I still have no idea how to do that correctly. I want to to use custom post type posts on a page with a slug which is the same to that custom posts type rewrite slug.

Naturally that causes slug conflict, is there a way to avoid that? I believe I have to make my rewrite rules, but I can't understand how to make it correctly.

I want to to get url like that: /PAGE_SLUG/CUSTOM_POST_TYPE_SLUG/ and I also want pagination to work. I've spent a couple of days trying to make it work, I used plugins, I used different sorts of queries, nothing helped. There was one positive result, but I think it was somehow incorrectly made rewrite rule, which was flushed once I entered permalinks page. If you created such functionality please tell me, how you did that?

I tried with a plugin assistance and via my own WP_Queries. For example I have a function which performs output of child pages on a certain page it works via WP_Query if(!is_numeric($id)) return false;

$args = array(
    'posts_per_page'   => $ppp,
    'orderby'          => 'menu_order',
    'order'            => 'ASC',
    'post_type'        => 'page',
    'post_parent'      => $id,
    'post_status'      => 'publish',
    'paged'            => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    'suppress_filters' => false 
);

$pages = new WP_Query( $args );

if (  $pages->have_posts() ) :

Then I other manipulations and output. List of pages is operable I can enter a certain page and pagination works fins.

Then I need to make a page which would perform output of CPT posts and I also want to be page slug undependable. For example today I want to be 'news' tomorrow I call it 'blog' and day after tomorrow I call it 'history'. I want that changing of page slug would automatically change CPT behavior. So it's custom posts could be opened and views on that page without extra manipulations. I started with basic output at first and made that function to

$args = array(
        'posts_per_page'   => $ppp,
        'orderby'          => 'meta_value_num',
        'meta_key'         => 'date',
        'order'            => 'DESC',
        'post_type'        => 'pt-news',
        'post_status'      => 'publish',
        'paged'            => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
        'suppress_filters' => false 
    );

    $pages = new WP_Query( $args );

    $news = '';

    foreach( $pages -> posts as $post ){

As you see main difference if post type. But behavior of even that basic thing is very different from pages. Either CPT can't be opened either pagination is broken. What I already tried:

  1. Making CPT rewrite slug to page's slug. Before permalinks are flushed it makes pagination work, but CPT can't be opened. After permalinks are flushed I can open CPT but pagination is broken, it redirects me to an empty 'Latests posts' page. To say the truth I don't know what it is.

  2. I tried plugin posts in pages, which worked fine with posts, I mean there was not problem with categorized posts, but it is not flexible and slud dependant way, so I rejected it.

  3. I tried different queries and manipulation with page/paged variable. It did not help.

  4. I tried to update rewrite rules and CTP rewrite slug on the fly. It does not help or I did in incorrectly. I worked from theme's functions.php, so I can't permanently change CPT's slug and I was not able to force WP to accept rewrite rules which I made there before calling flush_rewrite_rules();. Even when I call it I do not see my rewrite rules in debug panel. I wanted to add pagination rule but it does not work.

What I have now: a page which performs correct output of CPT with a slug similar to page's slug. They can be are accessible as pages, but pagination does not work it sends me latests posts. If I remove rewrite slug then a get wrong urls like CPT_SLUG/CPTP_SLUG they work and pagination is working but it is wrong. Any ideas how to make CPT to be workable on page's dinamyc slug? Or at least workable on a page page with static slug similar to CPT rewrite slug?

Upd.:

Milo offered to solve this problem via WP archive mechanism. I give details of post type with archive settings:

name: pt-news label: Новости singular_label: News description: public: true show_ui: true show_in_nav_menus: true show_in_rest: false rest_base: has_archive: true has_archive_string: news exclude_from_search: false capability_type: post hierarchical: false rewrite: true rewrite_slug: news rewrite_withfront: false query_var: true query_var_slug: menu_position: show_in_menu: true show_in_menu_string: menu_icon: custom_supports:

That does not help. I try to enter on /news/ URI and it simple gives me front page without any redirects, like front page and /news/ are the same URI.

If we find this way of solving can't work can anyone suggest a tool to set rewrite rules from admin page which will not flushed by Wordpress accidentally? I think if it is so problematic to make it work as planned I could at least make pagination rules applicable. If I set my CPT rewrite URL to news and page slug to news I am able to open CPT posts but pagination does not work, it sends me to loop.php which does not have a clue about my WP_Query.

  • What is the purpose of the page? Are you aware of post type archives, and why will that not work for you in this case? Either way, you're much more likely to get help if you show what research you've done and what you've actually tried. – Milo Apr 1 '16 at 14:53
  • My idea was to make a plugin which could connect custom post type posts with a certain page, so if page slug is changed that does not affect custom post type behavior. I tried to make such thing with pages I do WP_Query get pages and perform output. Pagination urls and other stuff work as it has to work. But when I try similar thing with custom post type either pagination is broken either custom post type is not working. I tried a plugin Posts in Pages which is supposed to work with custom posts type, but it does not. Then I tried couple of different queries and playing with page variable. – NIck Apr 1 '16 at 16:47
  • Pagination of what? A post archive? You can register a CPT with the slug of a page as rewrite slug and it works without issue, so I'm not sure where you are having trouble with that. As always here, it helps to add actual code you have tried to your question. – Milo Apr 2 '16 at 16:58
  • Milo, it does not work that way, that is main issue unexpected for me. For example I have a function which outputs subpages on a certain page $args = array( 'posts_per_page' => $ppp, 'orderby' => 'menu_order', 'order' => 'ASC', 'post_type' => 'page', 'post_parent' => $id, 'post_status' => 'publish', 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), 'suppress_filters' => false ); $pages = new WP_Query( $args ); I call it inside page template perform output and pagination works fins and pages can be opened. But if I only change it to CPT I get slug conflict – NIck Apr 2 '16 at 18:19
  • Edit your question and add more details and code there, code in comments is unreadable. – Milo Apr 2 '16 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.