0

I am working on a site for a client and am using a plugin that adds an "event" custom post type and an "event-category" taxonomy.

I have also attached the "event-category" taxonomy to the default Wordpress "post" post type using the register_taxonomy_for_object_type function.

Now I have the following working URL scheme:

  • Post are accessed using the URL structure site-name.com/article/post-slug
  • Events are accesed using the URL structure site-name.com/event/event-slug
  • Event category term archives have the URL structure site-name.com/events/event-category-slug

I made the event category term archives display only events by using a pre_get_posts action with the following code:

if (!$query->is_admin() && $query->is_main_query() && is_tax('event-category')) {
    $query->set('post_type', 'event');
}

My problem is that I want to also have category archive pages that show only posts.

If possible these would ideally be accessed with the URL site-name.com/articles/event-category-slug

But if that's not possible it would be OK to have a different URL, as long as it leads to a page listing just posts in that particular event category.

Any help with the rewrite rules needed to achieve this would be greatly appreciated. I've found some articles that describe similar problems but I haven't been able to make them work for me.

2 Answers 2

0

I think your issue is with the rewrite portion when you registered the custom taxonomy with posts.

'rewrite' => array('slug' => 'some-slug', 'with_front' => false)

You can replace some-slug with anything you would like. It might be possible to replace it with article/event-category to get the results you want.

Setting with_front to false removes blog as a prefix. If you have changed the blog prefix to article. Then you should be able to set with_front to true and replace some-slug with `event-category

Here is a link with more info on registering custom taxonomies https://clarknikdelpowell.com/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/

EDIT: Can you do the following completely untested, created example code from link below:

register_post_type(
    'post',
    array(
        'taxonomies' => array( 'event-category' )
    ),
    array(
        'rewrite' => array('slug' => 'some-slug', 'with_front' => false)
        // other arguments may be needed from other registration of taxonomy
    )
);

Reference: Can multiple custom post types share a custom taxonomy?

12
  • if you think that the question is a dup, please flag it as one ;) May 17, 2016 at 14:13
  • The title of the link is the summary, what would you like the answer to be moved to this page. Seems like this page should be marked as duplicate over saying my answer isnt good.
    – stoi2m1
    May 17, 2016 at 14:15
  • I changed my comments, didn't see where you were linking to at first ;) Usually outbound links are bad answers if not at least summerized May 17, 2016 at 14:17
  • How do I do that, I dont see mark as duplicate as a flag for me. Maybe my rep isnt high enough?
    – stoi2m1
    May 17, 2016 at 14:23
  • I took a look at that question, but it's not the same problem. In that question the issue is with having two taxonomies tied to one post type. In my case it's two post types sharing a taxonomy. Maybe I could adapt that answer but truthfully I don't know how.
    – Andreyu
    May 17, 2016 at 14:29
0

I eventually managed to find a solution:

First of all, the pre_get_posts action that I originally had wasn't good, because both the events and the posts are displayed using the same template (taxonomy-event-category.php). The code that I wrote in pre_get_posts forced wordpress to display only events in that template, so I removed it.

I replaced it with this code, which filters the query according to the post_type query var:

if (is_tax('event-category')) {
    $query->set('post_type', get_query_var('post_type', 'event'));
}

(In case there is not post_type query var, it defaults to events).

Then I added the following rewrite rule, which makes Wordpress recognize the articles/event-category-slug URLs

add_rewrite_rule('^articles/(?!page)(.+?)(?:/page/([0-9]*))*/?$', 'index.php?post_type=post&event-category=$matches[1]&paged=$matches[2]', 'top');

The (?!page) part of the regex is needed so that the rule doesn't trigger on paged URLs like articles/page/2, but still triggers on articles/event-category-slug/page/2

I'm not an expert in regex so maybe there's a better way to write that rule, but it seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.