Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created some custom post type let say:

  • post_type_1
  • post_type_2

Those post types are related to each other, every post_type_1 has parent of many post_type_2. Then I created a custom taxonomy for post_type_2, let's call it tax_1. The following are the code:

function registerPostType1() {
  $supports = array(
    'title', 'editor', 'thumbnail', 'excerpt',
    'comments', 'post-formats'
  );
  $args = array(
    'public' => true,
    'hierarchical' => false,
    'query_var' => false,
    'has_archive' => true,
    'rewrite' => array('slug' => 'posttype1'),
    'supports' => $supports
  );
  register_post_type('post_type_1', $args);
}


function registerPostType2() {
  $supports = array(
    'title', 'editor', 'author', 'thumbnail', 'excerpt',
    'comments', 'post-formats'
  );
  $args = array(
    'public' => true,
    'hierarchical' => false,
    'query_var' => false,
    'has_archive' => true,
    'rewrite' => array('slug' => 'post-type-2'),
    'taxonomies' => array('post_tag'),
    'supports' => $supports
  );
  register_post_type('post_type_2', $args);
}


function registerTax1() {
  $args = array(
    'hierarchical' => false,
    'query_var' => 'tax-1'
  );
  register_taxonomy('tax-1', 'post_type_2', $args);
}


add_action('init', 'registerPostType2');
add_action('init', 'registerTax1', 0);


function registerPostType1Query() {
  add_rewrite_tag('%posttype1%','([^&]+)');
}

function addPostType1QueryVar($query_vars) {
  $query_vars[] = 'posttype1';
  return $query_vars;
}

function modifyPostType2Query($query) {
  if ($query->is_main_query() &&
  haveTaxonomyQuery($query, 'tax-1')) {
    $query->set('orderby', 'rand'); // Randomly order the posts
    $query->set('posts_per_page', 1); // Show only one post

    $query->set('meta_key', 'posttype1');
    $query->set('meta_value', $query->query_vars['posttype1']);
  }
}

function haveTaxonomyQuery($query, $taxToFind) {
  if (!property_exists($query, 'tax_query') ||
  empty($query->tax_query) ||
  !property_exists($query->tax_query, 'queries')) {
    return FALSE;
  }

  foreach ($query->tax_query->queries as $index => $taxQuery) {
    if ($taxQuery['taxonomy'] === $taxToFind)
      return TRUE;
  }
  return FALSE;
}


add_action('init', 'registerPostType1Query');
add_filter('query_vars', 'addPostType1QueryVar');
add_action('pre_get_posts', 'modifyPostType2Query');

Now I'm trying to create posts for post_type_1 in which for each post_type_1 page they have a link to archive page of tax-1 (let say term-1 & term-2) in which the page is gonna be used to filter 1 random post under tax-1 (depends on which term was clicked) and have relationship with that previous post_type_1 post.

So in short it's like relationships between city (post_type_1), people (post_type_2) and gender (tax-1) then with male (term-1) and female (term-2).

Therefore I have:

  • single-post_type_1.php
  • single-post_type_2.php
  • taxonomy-tax-1.php

The following is the code to generate the link to the taxonomy-tax-1.php page, it's in single-post_type_1.php file:

<a href="<?php echo get_term_link('term-1', 'tax-1') ?>?posttype1=<?php the_ID(); ?>">
  <img src="<?php bloginfo('template_url'); ?>/images/term-1.jpg" />
</a>

At first the link showed me 404 page, then I refresh the permalink from Settings -> Permalinks and it was working well. However the next day, when I accessed the page again, it showed me the index page instead until I refresh the permalinks again.

I also tried installing a rewrite analyzer plugin like Monkeyman Rewrite Analyzer and tried comparing the rewrite between each (the working one and the error one which shows me the homepage). The different was the error one showed something like (on the tax-1 rewrite rule's substitution):

This query variable is not public and will not be saved

Does anyone has any idea what I did wrong? It's really annoying to have to refresh the permalinks every single time. Any help would be much appreciated.

Thanks in advance.

share|improve this question
1  
Despite this being a question about pretty URLs, you have not posted a single example URL of what you expect, and what you got. You've described your situation, but you havent told us what the problem actually is. "They revert to what exactly?" –  Tom J Nowell Apr 22 '13 at 11:27
    
Also I'd avoid setting post meta that has the same key as the post type, if not to prevent clashes, but to prevent confusion –  Tom J Nowell Apr 22 '13 at 11:27
    
Sorry about the unclear question. The problem is the every time I'm trying to access the page (descibed above, taxonomy-tax-1.php) it redirected me to index.php (home page). To solve the problem, I have to refresh the permalinks, but at later time, it becomes inaccessible again (revert), then I have to refresh permalinks again to fix it. That goes on until now. Regarding the example url, I don't really care about how the url is, I just want to make the taxonomy-tax-1.php accessible. Thanks –  verdy Apr 22 '13 at 11:47
    
ooh, check you havent got any automated flushing or flushing on init –  Tom J Nowell Apr 22 '13 at 13:44

1 Answer 1

Finally I found the solution. I think the problem was because I didn't specify the rewrite rules for taxonomy archive page manually because I thought that I just want some simple url (default) so I didn't have to add the rewrite rule at all.

So I tried the following code and add some flush rules hooked during activation of the theme:

add_rewrite_rule('tax-1/([^/]+)/\?posttype1=([0-9]+)?$', 'index.php?tax-1=$matches[1]&posttype1=$matches[2]', 'top');

The Monkeyman Rewrite Analyzer stating the following was also very helpful that I thought of that way:

This query variable is not public and will not be saved

It thus brought me to this link: how to properly rewrite url by custom var

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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