Ajax Dynamic Archives not showing correct results

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

Read More
<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
        });
    }
});
});
});

Related posts

2 comments

  1. 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;
    });
    });
    
  2. 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.

    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.

Comments are closed.