0

I am using form for my archive page so user can specify which posts they want to see and how they want to see them. I have it setup so they can choose ascending or descending order, posts per page etc, using $_GET in my forms. However I have also set up form to set display posts by year/month:

<select name="monthnum">

          <option value="null">Month</option>

          <option value="1">January</option>

          <option value="2">February</option>

          <option value="3">March</option>

etc. for all month and the same one for year

then my query arguments look like this:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

    $custom_args = array(

        'post_type' => 'post',
        'posts_per_page' => $_GET["posts-per-page"],
        'paged' => $paged,
        'order' => $_GET["order"],
        "orderby" => "date",
        "year" => $_GET["year"],
        "monthnum" => $_GET["monthnum"]

      );

    $custom_query = new WP_Query( $custom_args );

So far I added a couple of posts to test things so they are all august 2015 and if i set these through $_GET, it displays well however if I try to do January or any other month apart from August, my archive page gets redirected to the page with the loop which is index.php in my case and it display "no posts..." while it should stay on the same page and display "No posts..." on it. as I've set it up. There's no need to post rest of the code since I've figured out that the redirection happens immediately after new WP_query is created. What might cause this issue? Again $_GET ["posts per page"] and $_GET["order"] all work as expected.

  • Nevermind I resolved it by myself, changed the month select name from "monthnum" to "month" and "year" to "y" and then added them into query arguments if they are set like so: if ( ! empty( $_GET["month"]) ) { $custom_args["monthnum"] = $_GET["month"]; } if ( ! empty( $_GET["y"]) ) { $custom_args["year"] = $_GET["y"]; } – CoderJ Aug 20 '15 at 18:25
0

By using those named GET variables such as year or monthnum you effectively short circuit the URL rewrite (if you are using pretty permalinks). This way you end up querying the main WP_Query instance first and effectively making yours completely useless (well, you do alter the posts_per_page attribute so there's that). Anyway, the point is that there is a base class WP that issues a 404 when the main WP_Query instance doesn't return posts (which is the case) and the 404 template is invoked (if your theme has it) or if it's missing the index template is used (which seems to be confirmed by your question).

Now, the quickest fix is to basically rename your GET variables into q_year or q_monthnum for example.

Your Answer

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

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