WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#10616 closed defect (bug) (invalid)

post queries are not working with custom permalinks

Reported by: frumph Owned by:
Milestone: Priority: high
Severity: major Version: 2.8.4
Component: Template Keywords:
Focuses: Cc:

Description

With permalinks off this works:

 if (isset($_GET['archive_year'])) {
   $archive_year = (int)$_GET['archive_year'];
 }

With Permalinks on:

/%category%/%postname%/

It does not see the posted query, isset shows nothing.

This is what the dashboard - settings - permalinks asked me to put in the .htaccess (i had since made it rw)

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

I noticed as well that if I set it using the get_query_var and add the var to the vars list I couldn't get that to work at all with whatever tutorial I was reading.



Attachments (2)

archive-comic-year-original.php (2.4 KB) - added by frumph 5 years ago.
This is the template for comicpress 2.8:
archive-comic-year-new.php (2.5 KB) - added by frumph 5 years ago.
This is the new one to make it work.

Download all attachments as: .zip

Change History (22)

comment:1 follow-up: frumph5 years ago

Additional Info, this is my href line:

<a href="<?php echo add_query_arg('archive_year', $year) ?>"><strong><?php echo $year ?></strong></a> |

I am using the add_query_arg, remember this does work without permalink /%category%/%postname%/ turned on.

comment:2 frumph5 years ago

  • Version set to 2.8.4

comment:3 in reply to: ↑ 1 frumph5 years ago

Replying to frumph:

Additional Info, this is my href line:

<a href="<?php echo add_query_arg('archive_year', $year) ?>"><strong><?php echo $year ?></strong></a> |

I am using the add_query_arg, remember this does work without permalink /%category%/%postname%/ turned on.

erm turned off I meant, with no permalinks on

comment:4 follow-up: frumph5 years ago

I figured out how to make it work by doing:

	parse_str(preg_replace('#^.*\?#', '', $_SERVER['REQUEST_URI']), $query_vars);

Until it's fixed.

comment:5 in reply to: ↑ 4 filosofo5 years ago

Replying to frumph:

 	parse_str(preg_replace('#^.*\?#', '', $_SERVER['REQUEST_URI']), $query_vars);

I think you're taking a security risk by parsing arbitrary request arguments.

A better way is to use the WordPress rewrite API.

Attach something like the following to the "init" action, and trigger a flush of the rewrite rules (one way to do that is to visit the admin permalinks page).

add_rewrite_rule('^archives-year/(.*)/?$', 'index.php?archive_year=$matches[1]', 'top');
add_rewrite_tag('%archive_year%', '(.*)');

The first line prepares the rewrite rule so it will be added to the rules after the flush. The second makes WP and WP_Query aware that archive_year is a query argument.

Depending on what you're trying to do, the existing "year" query variable might be sufficient instead of the custom "archive_year". In that case, you could just do

add_rewrite_rule('^archives-year/(.*)/?$', 'index.php?year=$matches[1]', 'top');

I suggest this be marked as "won't fix," as we shouldn't be querying based on arbitrary request arguments, and the desired results can be obtained with the existing API.

comment:6 follow-up: frumph5 years ago

I think you're missing the point that a $_GET works with permalinks off, but doesnt with permalinks on.

comment:7 in reply to: ↑ 6 filosofo5 years ago

Replying to frumph:

I think you're missing the point that a $_GET works with permalinks off, but doesnt with permalinks on.

I can't reproduce that.

If I request something like

http://mysite.com/category-slug/post-slug/?archive_year=2009

And then put in index.php of the theme:

if (isset($_GET['archive_year'])) {
        $archive_year = (int)$_GET['archive_year'];
        print_r("the year is $archive_year");
}

It prints "the year is 2009". What are you doing differently?

comment:8 dd325 years ago

What is the result of using this: ?

<?php echo add_query_arg('archive_year', $year) ?>

I think you're attempting to use core function in a way it wasnt designed to be used, Either that, Or you've overriden something incorrectly.

Can you post the -exact- steps to reproduce with the default theme and no plugins? I want code i can cut and paste and reproduce it.

frumph5 years ago

This is the template for comicpress 2.8:

frumph5 years ago

This is the new one to make it work.

comment:9 frumph5 years ago

Templates Attached.

http://myextralife.com/ is the website currently using 2.8 and it doesnt work on it.
http://mofcomic.com/ is a website using 2.8 and it does work on it.

http://myextralife.com/ I had all plugins turned off and the plugin directory cleared when testing.

  • var_dumped $GLOBALS * var_dumped $wp->query_vars -- nada
  • $archive_year = $GLOBALSarchive_year?; wasn't there cause wasnt found in $globals.

Additional note: On the same site myextralife i'm doing a form post with the $_GET and *THAT* works which is also a template. However doing <?php echo add_query_arg('archive_year', $year) ?> does not.

Tried adding a public query_var using:

add_filter('query_vars', 'cp_add_query_vars');
 
function cp_add_query_vars( $qvars ){
	$qvars[] = 'archive_year';
	return $qvars;
}

ComicPress 2.8 (not released yet) http://comicpress.frumph.net/themes/comicpress-rc2.zip

Additionally it only happens on one out of maybe 4 different sites using the beta. This was functioning just fine before the 2.8ish releases as is found at http://comicpress.org/

You can find me at philip@… or http://webcomicplanet.com/chat/ and I can show you on myextralife.com and put any var_dumps you want to see what' it's doing.

(my extralife.com is currently using my band-aided version)

comment:11 dd325 years ago

I'm still failing to see the problem..

$GLOBALS['archive_year'] will be empty unless you set it. WordPress has overridden register_globals for a long time.

Adding it to query_vars will allow the use of get_query_var('archive_year') and the use of archive_year in rewrite rules.

var_dump($_GET['archive_year']);

works fine for me in the default themes header.php file.

comment:12 dd325 years ago

Sorry i missed the attachments you added.

Doesnt changing it from

<?php 
 if (isset($_GET['archive_year'])) {
   $archive_year = (int)$_GET['archive_year'];

to

<?php 
global $archive_year;
 if (isset($_GET['archive_year'])) {
   $archive_year = (int)$_GET['archive_year'];

help?

comment:13 frumph5 years ago

Setting it as global doesnt help, $archive_year is still empty.

You probably were thinking $archive_year would return the last value it was set but it does not, it's still empty. Why? Because (isset( returns false because it's thinking it's not set even though it's in the query line.

Dont know if this will help but THIS works:

  $archive_year = $_REQUEST['archive_year'];
  echo $archive_year;
		<form method="post" action="">
		<input type="hidden" name="archive_year" value="2001" />
		<button type="submit" value="submit" name="submit">Click Me.</button></form>

echo $archive_year will echo out 2001.

However, doing this doesnt work:

$archive_year = $_GET['archive_year'];

echo $archive_year;

<a href="http://www.domain.com/archive/comics-by-year/?archive_year=2001">2001</a>

$archive_year is "UNSET" if isset returns false if I were to add that.

the href which is:
<a href="<?php echo add_query_arg('archive_year', $year) ?>"><strong><?php echo $year ?></strong></a>

where $year == 2001;

.. Turning Permalinks Off

$archive_year = $_GET['archive_year'];

echo $archive_year;

<a href="http://www.domain.com/?page_id=12846&archive_year=2001">2001</a>

This works, $archive_year returns 2001

it's when I set the permalinks on it doesnt work on page-templates

tested on all templates that we use, same thing.

comment:14 follow-up: frumph5 years ago

Tested it in page.php by itself and not being a template. Same thing, anything in the request_uri line disappears and can't be found, so _GET / _REQUEST will not find anything when permalinks are enabled, however works when disabled.

comment:15 in reply to: ↑ 14 filosofo5 years ago

Replying to frumph:

Tested it in page.php by itself and not being a template. Same thing, anything in the request_uri line disappears and can't be found, so _GET / _REQUEST will not find anything when permalinks are enabled, however works when disabled.

Can you do the following?

  • Create a test page.
  • Create a page template named something like page-test.php that has the following content:
    <?php 
    /*
    Template Name: Test Template
    */
    echo '$_GET:';
    var_dump($_GET);
    
    echo '$_POST:';
    var_dump($_POST);
    
    echo '$_REQUEST:';
    var_dump($_REQUEST);
    
  • Assign the page-test.php template to the page and post the link here.

comment:17 filosofo5 years ago

I noticed that your header response says that the server is lighttpd, and in fact one can download that site's .htaccess file, so your server must be using different rewrite rules for the "pretty" permalinks to work at all. Could it be that those lighttpd rules are messing with $_GET settings?

comment:18 frumph5 years ago

@filesofo you bring up a seriously valid point that I did not look at and I appreciate that. Unfortunately I am just the programmer on these individuals sites and will have to contact server administrator to see if he has some server set rewrite rules in effect. Will keep in touch on it.

Documentation on lighthttpd's mod_rewrite:
http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModRewrite

comment:19 frumph5 years ago

  • Resolution set to invalid
  • Status changed from new to closed

We've decided to go back to using our original $_GET methods in the ComicPress 2.8 release and give documentation on how to make it work with Lighthttpd on the forums for those who need it.

Thanks @filesofo and @dd32 you guys are great. Finding out it indeed was not a wordpress problem, that Lighthttpd's configuration and ModReWrite does not play night with wordpress because of it's different rule convention.

comment:20 Denis-de-Bernardy4 years ago

  • Milestone Unassigned deleted
Note: See TracTickets for help on using tickets.