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.

IMPORTANT UPDATE: I've realised that my problems could also lie with the WordPress 3.3 update.


A while ago I posted a solution to my own question about searching Custom Post Types.

At that time, I was using the URL http://www.seriouslyfish.com/dev/ whilst I redeveloped the website.

Now I've "gone live" and moved everything over to http://www.seriouslyfish.com.

For some reason, this appears to have broken my Custom Post Type searches. If you try searching from the "PROFILESEARCH" or "QUESTIONSEARCH" boxes, you'll see that it's redirecting to /search/ rather than /questions/search or /species/search.

I've got rewrite analyzer installed, but I don't think that's the problem as this URL appears to work correctly: http://www.seriouslyfish.com/species/search/betta.

Any idea what could be causing this? I'm not 100% sure that the problem lies with the changing of the URL, as none of the code needed to change.


.htaccess

RewriteEngine On
RewriteCond %{QUERY_STRING} genus=([a-zA-Z0-9-]+)&species=([a-zA-Z0-9-]+)
RewriteRule ^profile.php$ /species/%1-%2? [L,R=301]

RewriteCond %{QUERY_STRING} id=([0-9]*)
RewriteRule ^profile.php$ calc.php?id=%1 [L,R=301]

RewriteRule ^kb.php$ /knowledge-base/ [L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

wp-config.php (or, a couple of the lines in it - nothing else relating to URLs exists in the file)

define('WP_SITEURL', 'http://www.seriouslyfish.com');
define('WP_HOME', 'http://www.seriouslyfish.com');

the form actions

PROFILESEARCH

<h1 class="profilesearch">PROFILE<span class="white">SEARCH</span></h1>
<form id="profilesearch" action="<?php echo home_url( '/species/' ); ?>" method="get">
    <input type="text" size="50" class="default-value" value="SEARCH" name="s" />
    <input type="submit" value="GO" class="profilesearch_submit" />
    <label style="width: 180px;">SEARCH FISH SPECIES PROFILES</label>
</form>

QUESTIONSSEARCH

<h1 class="profilesearch">QUESTION<span class="white">SEARCH</span></h1>
<form id="profilesearch" action="<?php echo qa_get_url('archive'); ?>" method="post">
    <input type="hidden" name="type" value="profile" />

    <input type="text" size="50" class="default-value" value="<?php the_search_query(); ?>" name="s" />
    <input type="submit" value="GO" class="profilesearch_submit" />
</form>

GLOSSARYSEARCH

<div class="glossary" href="#">
    GLOSSARY &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong class="dkblue">&#9660;</strong>
    <span>
        <form action="<?php echo home_url( '/glossary/' ); ?>" method="get">
            <p style="font-size:12px;">SEARCH<strong class="dkblue">GLOSSARY</strong></p>
            <input type="text" name="s" value="" />
            <input type="submit" value="GO" class="glossary_submit" />
        </form>
        <div class="glossaryletters">
            <a href="/glossary/a/">A</a>
...
            <a href="/glossary/z/">Z</a>
            <div class="clear"></div>
        </div>
    </span>
</div>

custom_rewrite function

/* --- rewrite rules for searches...*/
function custom_rewrite( $wp_rewrite ) {
    $species = array(
        '(species|glossary)/search/(.+?)(/page/([0-9]+))?/?$'      =>  'index.php?post_type='.$wp_rewrite->preg_index(1).'&s='.$wp_rewrite->preg_index(2).'&paged='.$wp_rewrite->preg_index(4)
    );  

    $wp_rewrite->rules = $species + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( 'generate_rewrite_rules', 'custom_rewrite' );

Thanks in advance,

share|improve this question
    
Are you registering the post types yourself, or using a plugin? If the former, are you adding any additional rewrite rules yourself to match a combined post type & search request? If the latter, is there an option that enables this feature? By default, WP won't generate a rule for the circumstance, which is why redirect_canonical attempts to "fix" things and redirects the search to root. –  TheDeadMedic Jul 28 '12 at 15:21
    
Hi mate, the code I'm using for the redirects etc. can be found in the solution I posted to my own question here: wordpress.stackexchange.com/a/47462/6344 –  dunc Jul 28 '12 at 16:14
    
Add remove_action( 'template_redirect', 'redirect_canonical' ) to your functions, and see if that stops the redirect - if it does, we've found our troublemaker ;) –  TheDeadMedic Jul 28 '12 at 18:03
    
your question redirect is working, even if it's not the way you want: search/?s=fish redirects to questions/search/?s=fish. Also please post your custom_rewrite() function. –  peteroak Jul 29 '12 at 2:05
    
Re: Important Update - What about now, with 3.4.1? –  brasofilo Jul 29 '12 at 11:09
show 1 more comment

4 Answers

It's possible that, after moving the site, you hadn't updated WordPress' understanding of where the site is located. You might want to try adding the following to your wp-config.php file:

define('WP_SITEURL', 'http://www.seriouslyfish.com');
define('WP_HOME', 'http://www.seriouslyfish.com');

This will override the settings in the database. WordPress uses the location settings to direct rewrite traffic, so this might solve the problem. I'm guessing this because you say that things were working properly before you moved the site.

Maybe it would make sense to try re-checking your .htaccess file as well? Maybe something there is redirecting improperly?

It might be a good idea as well to check the action URL on the form. Is that being generated by the a plugin, WordPress proper, or by something else? If it's under your control and can be re-written somehow, maybe you don't need the manual redirection being used in the answer you link to at the top of your question?

Sorry if I'm grasping at straws here, but it's difficult to debug this without being able to poke the code and see what's going on.

share|improve this answer
    
Hi Gavin. Thanks for that - I've included all of the relevant code, apart from that which is discussed in the answer to my original post (linked in the first paragraph). When I moved the site I made sure there was no mention of /dev in either the database or any files used in the WP install. As such, and as I mentioned, I'm not convinced that the problem lies with the change of URL. I just can't work out what else it could be.. –  dunc Jun 10 '12 at 21:27
add comment

Have you try to add EP_PERMALINK as ep_mask (end point mask) on your custom post type arg.? This parameter might be useful.

'permalink_epmask' => EP_PERMALINK

I was experience this once and resolve by just re-flushing permalink.

Good Luck.

share|improve this answer
add comment

Check your post types creation codes, sometimes people uses old code and WordPress has changed a lost since. No matter what you are using for search, your posts types should allow the following:

'exclude_from_search'=> false
'publicly_queryable'=> true
'has_archive'=> true 

Abou the search, I find this approach interesting: http://dbaines.com/blog/archive/wordpress-custom-post-type-multiple-search/

share|improve this answer
    
Thanks Diana. As per link to original post, all of those attributes are set correctly. –  dunc Jun 14 '12 at 14:21
    
I tried your profile search and this worked: <form id="profilesearch_form" action="<?php echo home_url( '/' ); ?>" method="get"> <input type="hidden" name="hotel"/> <input type="text" size="50" class="profilesearch_input" name="hotel" /> <input type="submit" value="GO" class="profilesearch_submit" /> </form> Where hotel is the post type. Quite strange because the resulting link goes: ?hotel=&hotel=the-input-value anything to do with ?s=. –  Diana Jun 14 '12 at 16:26
    
Hi Diana. I'm not sure what you're trying to explain by pasting me that code. What do you suspect the problem to be? –  dunc Jun 14 '12 at 20:49
    
I suspect you don't need any special action in .htaccess anymore (your previous question that lead to this one), WordPress now is able to rewrite links for posts types and taxonomies and you can build your structure by using filters. the .htaccess file is very drastic. You should try to keep the .htaccess as WordPress recommends. –  Diana Jun 15 '12 at 2:40
    
I believe the .htaccess is the default WordPress file, with a few 403 permanent redirects from my old URL structure? –  dunc Jun 15 '12 at 10:43
add comment

Have you tried flushing the permalinks by re-saving them, I had this problem and it worked for me.

Have you also looked at defining the search, so looking at what exactly you are searching try adding this function;

function mySearchFilter($query) {
    $post_type = $_GET['type'];
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
    };
    return $query;
};

add_filter('pre_get_posts','mySearchFilter');
share|improve this answer
    
Yeah, tried flushing the links. Will investigate your code later, though it doesn't explain why my searches randomly broke - they were working as intended. –  dunc Jun 15 '12 at 10:46
    
Adding the post_type to any will return attachements, which I'm guessing is not what they want. –  Cristian Jul 25 '12 at 5:09
add comment

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.