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.

Im baffled on why this is not showing the correct results, when selecting the month alone and say 100 posts are on the site, only 3 show up, then if you select the category and month the proper show up.

here is the archives template

<div id="archive-browser">
<div>
<h4>Month</h4>

<select id="month-choice">
<option val="no-choice"> &mdash; </option>
<?php wp_get_archives(array('type'    => 'monthly', 'format'  => 'option')); ?>
</select>
</div>

<div>
<h4>Category</h4>

<?php wp_dropdown_categories('show_option_none= -- ');?> 
</div>
</div>

<div id="archive-wrapper">
<div id="archive-pot"></div>
</div>

here is the archives getter

<?php

/*
    Template Name: Archives Getter
*/

$year = trim($_POST['digwp_y']);
$month = trim($_POST['digwp_m']);
$cat = trim($_POST['digwp_c']);
$querystring = "year=$year&monthnum=$month&cat=$cat&posts_per_page=-1";
query_posts($querystring); 

?>

<?php if (($year == '') && ($month == '') && ($cat == '-1')) { ?>

<table id="archives-table"><tr><td style='text-align: center; font-size: 15px; padding: 5px;'>Please choose from above.</td></tr></table>

<?php } else { ?>

<table id="archives-table">
    <?php    
        if (have_posts()) : while (have_posts()) : the_post(); ?>
            <tr>
                <td><img src="<?php echo get_post_meta($post->ID, 'PostThumb', true); ?>" alt="" style="width: 35px;" /></td>
                <td><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></td>
                <td><?php comments_popup_link(' ', '1 Comment', '% Comments'); ?></td>
                <td><?php the_date('m/j/Y'); ?></td>
            </tr>
    <?php 
        endwhile; else:

            echo "<tr><td style='text-align: center; font-size: 15px; padding: 5px;'>Nothing found.</td></tr>";

        endif; 
    ?>
</table>

and finally the jquery code

jQuery(function($) {
$("#archive-wrapper").height($("#archive-pot").height());
$("#archive-browser select").change(function() {
$("#archive-pot")
    .empty()
    .html("<div style='text-align: center; padding: 30px;'>loading...</div>");

var dateArray = $("#month-choice").val().split("/");
var y = dateArray[3];
var m = dateArray[4];
var c = $("#cat").val();

$.ajax({
    url: "/archive-getter/",
    dataType: "html",
    type: "POST",
    data: ({
        "digwp_y": y,
        "digwp_m" : m,
        "digwp_c" : c
    }),
    success: function(data) {
        $("#archive-pot").html(data);
        $("#archive-wrapper").animate({
            height: $("#archives-table tr").length * 50
        });
    }
});
});
});
share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

this is what i ended up with which works nicely.

functions.php part

<?php

function scripts_enqueue() {
if(is_page('archives')) {
    wp_enqueue_script('ajax_dropdown', get_stylesheet_directory_uri() . '/js/loadposts.js',array('jquery'));
    wp_localize_script( 'ajax_dropdown', 'myajax', array('custom_nonce' => wp_create_nonce('nonce-ajax-dropdown'), 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
}

add_action( 'wp_enqueue_scripts', 'scripts_enqueue' );

function wp_ajax_load_posts(){

if(!wp_verify_nonce( $_GET['_wpnonce'], 'nonce-ajax-dropdown'))
    die( 'Go away!' );

$args = array(
    'year' => trim($_GET['year']),
    'monthnum' => trim($_GET['month']),
    'posts_per_page' => -1,
    'orderby' => 'date',
    'cat' => trim($_GET['cat'] != "-1") ? trim($_GET['cat']) : 0,
);

$ajaxsort = new WP_Query($args);
?>
<table id="archives-table">
    <?php if ($ajaxsort->have_posts()) : while ($ajaxsort->have_posts()) : $ajaxsort->the_post();?>
            <tr>
                <td><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></td>
                <td><?php the_time('m/j/Y'); ?></td>
        <td><?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></td>
        <td><?php the_category();?>
            </tr>
    <?php 
        endwhile; else:
            echo "<tr><td style='text-align: center; font-size: 15px; padding: 5px;'>Nothing found.</td></tr>";
        endif; 
    ?>
</table>
<?php
    exit;
}

add_action('wp_ajax_load_posts', 'wp_ajax_load_posts');
add_action('wp_ajax_nopriv_load_posts', 'wp_ajax_load_posts');

jquery part

jQuery(document).ready(function($) {
$("#archive-browser select").change(function() {
    $(".message").hide();
    $("#archive-content").empty().html("<div style='text-align: center; padding: 30px;'><img src='http://i.imgur.com/TA3o5.gif' /></div>");
    var date = $('#month-choice option:selected').val();
    var dateArray = $("#month-choice").val().split("/");
    var year = dateArray[3];
    var month = dateArray[4];
    var cat = $('#cat').val();
    $.ajax({
        url: myajax.ajaxurl,
        type: 'GET',
        data: {
            action: 'load_posts',
            _wpnonce: myajax.custom_nonce,
            cat: cat,
            month: month,
            year: year,
        },
        success: function(data) {
            if (date == 'no-choice' && cat == "-1") {
                $("#archive-content").empty().html('<table class="message" id="archives-table"><tr><td style="text-align: center; font-size: 15px; padding: 5px;">Please choose from above.</td></tr></table>');
            } else {
                $("#archive-content").empty().html(data);
            }
        }
    });
    return false;
});
});
share|improve this answer
add comment

It is going to be difficult to debug something like this since I don't have your data on my server but you are doing several things that strike me as dicey.

First, you are using query_posts. Do not use query_posts.

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

http://codex.wordpress.org/Function_Reference/query_posts (emphasis mine)

Second, you are running your query before you check to see if you want to run a query. That is, before this line:

<?php if (($year == '') && ($month == '') && ($cat == '-1')) { ?>

Why would you run a query and then check to see whether you want to tell user they haven't filled in all the requirements they for the query to run?

Third, this...

if (($year == '') && ($month == '') && ($cat == '-1')) {

... is a strange condition. If is false if $cat is empty. Presumably, your form sets that value to -1 but that brings me to...

Fourth, you appear to be trusting user supplied data 100%. POST is no more (maybe 1% more) secure than GET. Unless there is validation somewhere not posted, a malicious user could potentially push unsafe data through that query.

Fifth, you are not using the AJAX API and this is exactly the kind of thing it is meant for.

share|improve this answer
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.