Jump to content

samoht

Members
  • Content Count

    421
  • Joined

  • Last visited

Community Reputation

0 Neutral

About samoht

  • Rank
    Advanced Member

Contact Methods

  • Website URL
    http://omegaopc.org

Profile Information

  • Gender
    Not Telling
  1. I fixed my JSON. I should not have been double encoding so it now looks like this: { "page": 1, "total_pages": 2, "sermons": [ { "ID": "165", "post_author": "1", "post_date": "2014-01-29 10:45:00", "post_date_gmt": "2014-01-29 21:06:20", "post_content": "The gospel grammar of the New Testament is not new - but perfectly structured in the judgment oracle against Philistia. In the final verse the great apologetic question, \"what shall one tell...\" is answered with both the indicative (The Lord has founded Zion) and the imperative (the afflicted of His people shall rest in it). This is the essential Q&A of all life!", "post_title": "The Lord Has Founded Zion", "post_excerpt": "", "post_status": "publish", "comment_status": "open", "ping_status": "open", "post_password": "", "post_name": "the-lord-has-founded-zion", "to_ping": "", "pinged": "", "post_modified": "2014-01-29 10:45:00", "post_modified_gmt": "2014-07-01 19:07:13", "post_content_filtered": "", "post_parent": "0", "guid": "http://localhost/newomega/?post_type=mbsb_sermon&p=165", "menu_order": "0", "post_type": "mbsb_sermon", "post_mime_type": "", "comment_count": "0" }, { "ID": "287", "post_author": "1", "post_date": "2014-06-29 10:45:00", "post_date_gmt": "2014-06-29 10:45:00", "post_content": "Isaiah begins his conclusion to the book of the king with an historical narrative that demonstrates the certainty and trustworthy nature of God's Word. ", "post_title": "In Whom Are You Trusting", "post_excerpt": "", "post_status": "publish", "comment_status": "open", "ping_status": "closed", "post_password": "", "post_name": "in-whom-are-you-trusting", "to_ping": "", "pinged": "", "post_modified": "2014-06-29 10:45:00", "post_modified_gmt": "2014-06-29 10:45:00", "post_content_filtered": "", "post_parent": "0", "guid": "http://localhost/newomega/?post_type=mbsb_sermon&p=287", "menu_order": "0", "post_type": "mbsb_sermon", "post_mime_type": "", "comment_count": "0" } ] } which is still valid JSON and is still giving me the SyntaxError : Unexpected token < I don't see where that could be coming from?
  2. ok I changed my PHP function to this: $serms = json_encode($sermons); $return = json_encode(array('page' => $paged, 'total_pages' => $max_num_pages, 'sermons' => $serms )); which spits out: { "page": 1, "total_pages": 2, "sermons": "[{\"ID\":\"165\",\"post_author\":\"1\",\"post_date\":\"2014-01-29 10:45:00\",\"post_date_gmt\":\"2014-01-29 21:06:20\",\"post_content\":\"The gospel grammar of the New Testament is not new - but perfectly structured in the judgment oracle against Philistia. In the final verse the great apologetic question, \\\"what shall one tell...\\\" is answered with both the indicative (The Lord has founded Zion) and the imperative (the afflicted of His people shall rest in it). This is the essential Q&A of all life!\",\"post_title\":\"The Lord Has Founded Zion\",\"post_excerpt\":\"\",\"post_status\":\"publish\",\"comment_status\":\"open\",\"ping_status\":\"open\",\"post_password\":\"\",\"post_name\":\"the-lord-has-founded-zion\",\"to_ping\":\"\",\"pinged\":\"\",\"post_modified\":\"2014-01-29 10:45:00\",\"post_modified_gmt\":\"2014-07-01 19:07:13\",\"post_content_filtered\":\"\",\"post_parent\":\"0\",\"guid\":\"http:\\/\\/localhost\\/newomega\\/?post_type=mbsb_sermon&p=165\",\"menu_order\":\"0\",\"post_type\":\"mbsb_sermon\",\"post_mime_type\":\"\",\"comment_count\":\"0\"},{\"ID\":\"287\",\"post_author\":\"1\",\"post_date\":\"2014-06-29 10:45:00\",\"post_date_gmt\":\"2014-06-29 10:45:00\",\"post_content\":\"Isaiah begins his conclusion to the book of the king with an historical narrative that demonstrates the certainty and trustworthy nature of God's Word. \",\"post_title\":\"In Whom Are You Trusting\",\"post_excerpt\":\"\",\"post_status\":\"publish\",\"comment_status\":\"open\",\"ping_status\":\"closed\",\"post_password\":\"\",\"post_name\":\"in-whom-are-you-trusting\",\"to_ping\":\"\",\"pinged\":\"\",\"post_modified\":\"2014-06-29 10:45:00\",\"post_modified_gmt\":\"2014-06-29 10:45:00\",\"post_content_filtered\":\"\",\"post_parent\":\"0\",\"guid\":\"http:\\/\\/localhost\\/newomega\\/?post_type=mbsb_sermon&p=287\",\"menu_order\":\"0\",\"post_type\":\"mbsb_sermon\",\"post_mime_type\":\"\",\"comment_count\":\"0\"}]" } I ran that through a json validator and it came back valid JSON So I know I giving my AJAX response valid JSON but it still is choking. My length is a much more respectable 6382 but I get Uncaught SyntaxError: Unexpected token < How can I get a SyntaxError if my JSON is valid?
  3. Also, If I just return the db query like $return = json_encode($sermons); then I get a length of 1752 and the log spits out the titles as expected ??
  4. Thanks for you patience with me. This is what I have changed my PHP function to look like to send JSON data back to my ajax if($_POST['meta_key']){ $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $mk = '"'.$_POST['meta_key'].'"'; $mv = intval($_POST['meta_value']); $ppp = intval($_POST['posts_per_page']); $offset = ($paged - 1) * $ppp; $sermons = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS sermons.* FROM {$wpdb->prefix}posts AS sermons, {$wpdb->prefix}postmeta WHERE meta_key = {$mk} AND meta_value = {$mv} AND sermons.ID = {$wpdb->prefix}postmeta.post_id LIMIT {$offset}, {$ppp}", OBJECT); $sql_posts_total = $wpdb->get_var( "SELECT FOUND_ROWS();" ); $max_num_pages = ceil($sql_posts_total / $ppp); $return = json_encode(array('page': $paged, 'total_pages': $max_num_pages, 'sermons': $sermons)); echo $return; exit; } I believe that this is sending back the multi-dimesional array with all the data I requested, but now I do not know how to parse that to populate my page or the navigation? I assume that I need to loop with .each? $.post(mbsb_ajaxurl, data, function(response) { console.log(response.length); $.each($.parseJSON(response), function(idx, obj) { console.log(obj.post_title); //$('.main_title').html(obj.post_title); }); }); but this returns a length of 187 (seems a little low? but it could be right) and a Uncaught SyntaxError: Unexpected token < I guess that means my JSON array is not correct?
  5. I see. Sorry for the misunderstanding. That is why you started with $return = array (... I am still a little foggy on how this would work with populating the page and the links. For example: you said 'page' => 2, at the beginning of your example array. I'm not sure where the 2 would come from. Would that be a php variable passed into the return array or would it be derived from somewhere else? Again, forgive me if I am missing the obvious. Thanks,
  6. Sorry the above code has an error in it, I knew it was there but posted to soon and it would not let me edit? Anyway the error is $('#pagination').load(lp); on line 133 But I removed that and still no luck. Am I correct in thinking that I should have an on click event for the link to be sure I serve up the correct paginated content? Otherwise it would just default to the normal php page right??
  7. Well I tried something else which I thought would work, but not so much. I added another ajax call to load the pagelinks //Listen for the menu's to change except the main filter_by dropdown var ids = ['filter_preacher_dropdown', 'filter_sort_by_dropdown', 'filter_per_page_dropdown', 'filter_series_dropdown', 'filter_service_dropdown', 'filter_tag_dropdown', 'filter_book_dropdown', 'filter_year_dropdown']; $('#' + ids.join(',#')).change(function(e) { var pt = [ "preacher","series","service" ]; if($.inArray(this.name, pt)!==-1){ var mk = this.name; var mv = $(this).val(); var ppp = $("#filter_per_page_dropdown").val(); var ob = $("#filter_sort_by_dropdown").val(); var lp = $(location).attr('href'); var data = { action: 'filter_sermons', meta_key: mk, meta_value: mv, posts_per_page: ppp, linkpage: lp }; $.post(mbsb_ajaxurl, data, function(response) { $('#sermonlists').fadeOut('slow', function() { $(this).html(response); $('#pagination').load(lp); }).fadeIn('slow'); }); } }); //NEW AJAX call for pagelinks $('#pagination').on('click', '#pag-link > li > a', function(){ var link = $(this).attr('href'); $.ajax({ url: $location().attr('href'), type:'GET', dataType: 'json', success: function(listings){ $('#pagination').html(listings); } // End of success function of ajax form }); // End of ajax call return false; }); but that just filled my #pagination div with the entire page again?
  8. Thanks Psycho, forgive me for this probably dumb question, but are you saying skip the php function all together?
  9. Hello all, I feel pretty good that I got my AJAX filter working properly. But it still needs some help. Now I am returning the content with Ajax that I want from the search filter of dropdown option (keep in mind there are several options to filter by). But the content returned is often to much to fit on one page - so I need to paginate the successfully returned html from my AJAX call. Here is where I get stumped. Do I setup a normal page nav outside the AJAX area? </div><!-- AJAX return container--> <nav class="pagenav"> <ul id="pag-link"> <li class="old"><?php next_posts_link('« Older Sermons', $sermons_query->max_num_pages) ?></li> <li class="new"><?php previous_posts_link('Newer Sermons »', $sermons_query->max_num_pages) ?></li> </ul> </nav> IF so, how do I update the links info? or do I keep the page nav inside the AJAX area but use some custom code to retrieve the proper link? (I tried using the normal wordpress page nav functions in my php function but they provide the url of the page that the php function is on, not the actual page being viewed). Do I need to make a separate AJAX call to a function that just builds my links? or is there a way to do it with my main call? Here is my AJAX call: //Listen for the menu's to change except the main filter_by dropdown var ids = ['filter_preacher_dropdown', 'filter_sort_by_dropdown', 'filter_per_page_dropdown', 'filter_series_dropdown', 'filter_service_dropdown', 'filter_tag_dropdown', 'filter_book_dropdown', 'filter_year_dropdown']; $('#' + ids.join(',#')).change(function(e) { var pt = [ "preacher","series","service" ]; if($.inArray(this.name, pt)!==-1){ var mk = this.name; var mv = $(this).val(); var ppp = $("#filter_per_page_dropdown").val(); var ob = $("#filter_sort_by_dropdown").val(); var data = { action: 'filter_sermons', meta_key: mk, meta_value: mv, posts_per_page: ppp }; $.post(mbsb_ajaxurl, data, function(response) { $('#sermonlists').fadeOut('slow', function() { $(this).html(response) }).fadeIn('slow'); }); } and here is my php function (slightly paired down for viewing ease): //ajax for sermons filter add_action('wp_ajax_filter_sermons', 'check_ajax'); add_action('wp_ajax_nopriv_filter_sermons', 'check_ajax'); function check_ajax() { global $wpdb, $paged, $max_num_pages; //check if filter is on post_type if($_POST['meta_key']){ $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $mk = '"'.$_POST['meta_key'].'"'; $mv = intval($_POST['meta_value']); $ppp = intval($_POST['posts_per_page']); $offset = ($paged - 1) * $ppp; $sermons = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS sermons.* FROM {$wpdb->prefix}posts AS sermons, {$wpdb->prefix}postmeta WHERE meta_key = {$mk} AND meta_value = {$mv} AND sermons.ID = {$wpdb->prefix}postmeta.post_id LIMIT {$offset}, {$ppp}", OBJECT); $sql_posts_total = $wpdb->get_var( "SELECT FOUND_ROWS();" ); $max_num_pages = ceil($sql_posts_total / $ppp); }elseif($_POST['name']){ //another custom query }elseif ($_POST['book_number']){ //another custom query } $posts = get_posts(); // returns posts in an array if($sermons) { global $post; foreach ($sermons as $post){ setup_postdata($post); //custom query info returned here } //Would the Pagination for the AJAXED content go here? }else{ ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php } die(); }
  10. samoht

    data is 0?

    yes, That just returned POST http://localhost/newomega/wp-admin/admin-ajax.php net::ERR_CONNECTION_RESET
  11. samoht

    data is 0?

    I realized that I had the check_ajax() function in the wrong place. So I moved it and the calls to it, and now I have the AJAX working (I think) - but the problem is what I am returning. function check_ajax() { $mk = '"'.$_POST['meta_key'].'"'; $mv = intval($_POST['meta_value']); $ppp = intval($_POST['posts_per_page']); $query = new WP_Query( array( 'meta_key' => $mk, 'meta_value' => $mv, 'post_type' => 'mbsb_sermon', 'posts_per_page' => $ppp ) ); $posts = get_posts(); // returns posts in an array echo json_encode($posts); die(); } returns [] while echo json_encode($query); returns {"query":{"meta_key":"\"preacher\"","meta_value":365,"post_type":"mbsb_sermon","posts_per_page":0},"query_vars":{"meta_key":"\"preacher\"","meta_value":365,"post_type":"mbsb_sermon","posts_per_page":10,"error":"","m":"","p":0,"post_parent":"","subpost":"","subpost_id":"","attachment":"","attachment_id":0,"name":"","static":"","pagename":"","page_id":0,"second":"","minute":"","hour":"","day":0,"monthnum":0,"year":0,"w":0,"category_name":"","tag":"","cat":"","tag_id":"","author":"","author_name":"","feed":"","tb":"","paged":0,"comments_popup":"","preview":"","s":"","sentence":"","fields":"","menu_order":"","category__in":[],"category__not_in":[],"category__and":[],"post__in":[],"post__not_in":[],"tag__in":[],"tag__not_in":[],"tag__and":[],"tag_slug__in":[],"tag_slug__and":[],"post_parent__in":[],"post_parent__not_in":[],"author__in":[],"author__not_in":[],"ignore_sticky_posts":false,"suppress_filters":false,"cache_results":true,"update_post_term_cache":true,"update_post_meta_cache":true,"nopaging":false,"comments_per_page":"50","no_found_rows":false,"order":"DESC"},"tax_query":{"queries":[],"relation":"AND"},"meta_query":{"queries":[{"key":"\"preacher\"","value":365}],"relation":"AND"},"date_query":false,"request":"SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'mbsb_sermon' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending') AND ( (wp_postmeta.meta_key = '\\\"preacher\\\"' AND CAST(wp_postmeta.meta_value AS CHAR) = '365') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10","posts":[],"post_count":0,"current_post":-1,"in_the_loop":false,"comment_count":0,"current_comment":-1,"found_posts":0,"max_num_pages":0,"max_num_comment_pages":0,"is_single":false,"is_preview":false,"is_page":false,"is_archive":true,"is_date":false,"is_year":false,"is_month":false,"is_day":false,"is_time":false,"is_author":false,"is_category":false,"is_tag":false,"is_tax":false,"is_search":false,"is_feed":false,"is_comment_feed":false,"is_trackback":false,"is_home":false,"is_404":false,"is_comments_popup":false,"is_paged":false,"is_admin":true,"is_attachment":false,"is_singular":false,"is_robots":false,"is_posts_page":false,"is_post_type_archive":true,"thumbnails_cached":false}
  12. samoht

    data is 0?

    Still no page change? Do I have to use my_sermon_query() on the main page? or does check_ajax() take care of that? I think I am missing only a small piece - but its still stopping the works. I have a main page (small-sermons.php) that queries the WPDB for all posts with the custom post_type=mbsb_sermon On that page I have included a form that allows the users to filter/search based on series, preacher, service - which are all custom post_types themselves. So I have a JQuery/Ajax call (frontend.js) that fires when those dropdowns change so I can handle resetting the query with the new parameters by sending the results to a custom function (functions.php) check_ajax(). I seem to be getting the right variables in the $_POST object to create the proper query for the search - but its not updating the page with the new query??
  13. samoht

    data is 0?

    I slightly rewrote the ajax and now I do get a return on the console $.ajax ( { type: "POST", url: ajax_object.ajaxurl, data: {action: 'filter_sermons', meta_key: mk, meta_value: mv, post_per_page: ppp}, success: function(data){ console.log(data); } } ); but still nothing is happening on the page?
  14. samoht

    data is 0?

    OK, thanks for the help understanding but I am not quite there. So the AJAX is ok? ( I changed the php function as noted but I still get 0 on the console log ) If so does that mean I just need to call the function from my page as the main query? $query = check_ajax(); ?> <?php while ( $query->have_posts() ) : $query->the_post(); ?> that doesn't seem right, because I have no default posts until the filter by or do I put the while loop for the posts in the check_ajax() function?
  15. samoht

    data is 0?

    Thanks Guru, But if I use 'results' I either get undefined - or I get 0 again?? data: { action: 'filter_sermons', meta_key: mk, meta_value: mv }, dataType: 'JSON', success:function(data){ console.log(results); } Gets me an Uncaught ReferenceError: results is not defined and data: { action: 'filter_sermons', meta_key: mk, meta_value: mv }, dataType: 'JSON', success:function(results){ console.log(results); } gets me a 0 ??
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.