Note that there are some explanatory texts on larger screens.

plurals
  1. PO$query_string in wordpress is returning null?
    text
    copied!<p>I'm trying to make a pagination system, and to make it work on every page I'm using the $query_string variable, which supposedly contains all the information about the category, etc.</p> <p>So I'm doing something like this:</p> <pre><code>add_action('wp_ajax_and_action', 'get_posts_page'); add_action('wp_ajax_nopriv_and_action', 'get_posts_page'); function get_posts_page() { $query_string = $_POST['query_string']; global $wpdb; query_posts($query_string . '&amp;posts_per_page=10&amp;post_status=publish&amp;offset='.$_POST['off']); </code></pre> <p>It's in my functions.php file. I've globalized the <code>$query_string</code> variable in my header.php file. The <code>$_POST['query_string']</code> is coming from a javascript function (also in my functions.php file) which I've set to be in wp_head (so the head of the document I assume). It's posting a bunch of data to the PHP function:</p> <pre><code>$.post('&lt;?php bloginfo('siteurl') ?&gt;/wp-admin/admin-ajax.php', { action: 'and_action', off: offset+number, pagenumber: page_number, query_string: '&lt;?php echo $query_string; ?&gt;' }, function(data) { </code></pre> <p>However, upon further inspection it shows the query_string variable as null. So when I do: <code>&lt;?php echo $query_string ?&gt;</code> nothing returns. Any ideas why that might be? Thanks :)</p> <hr> <h2>Updated</h2> <p>Heres an Update</p> <h2>functions.php</h2> <p>The Javascript:</p> <pre><code> add_filter('wp_head', 'javascript_page'); function javascript_page() { ?&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { var number = 10; var offset = 0; var page_number = 2; var busy = false; /* Bind the scroll function to an event */ $(window).bind('scroll', function(e) { /* If the scroll height plus the window height is more than the document height minus 10, continue */ if($(window).scrollTop() + $(window).height() &gt; $(document).height() - 10 &amp;&amp; !busy) { busy = true; /* Quick message so you know more stuff is loading */ $('.loading-more').html('Click to load more posts..'); $.post('&lt;?php bloginfo('siteurl') ?&gt;/wp-admin/admin-ajax.php', { action: 'and_action', off: offset+number, pagenumber: page_number, query_string: '&lt;?php echo $query_string; ?&gt;' }, function(data) { offset = offset+number; $('.empty-div').append('&lt;div class="pages"&gt;&lt;p&gt;Welcome to &lt;strong&gt;Page '+page_number+'&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;&lt;hr /&gt;'+data); busy = false; page_number += 1; }); } }); $('.loading-more').bind('click', function(e) { busy = true; $('.loading-more').html('&lt;em&gt;Loading more posts..&lt;/em&gt;') /* Quick message so you know more stuff is loading */ $.post('&lt;?php bloginfo('siteurl') ?&gt;/wp-admin/admin-ajax.php', { action: 'and_action', off: offset+number, pagenumber: page_number, query_string: '&lt;?php echo $query_string; ?&gt;' }, function(data) { offset = offset+number; $('.empty-div').append('&lt;div class="pages"&gt;&lt;p&gt;Welcome to &lt;strong&gt;Page '+page_number+'&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;&lt;hr /&gt;'+data); busy = false; page_number += 1; $('.loading-more').html('Click to load more posts..'); }); }); }); &lt;/script&gt; </code></pre> <p>The PHP function:</p> <pre><code>add_action('wp_ajax_and_action', 'get_posts_page'); add_action('wp_ajax_nopriv_and_action', 'get_posts_page'); function get_posts_page() { $query_string = $_POST['query_string']; global $wpdb; query_posts($query_string . '&amp;posts_per_page=10&amp;post_status=publish&amp;offset='.$_POST['off']); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?&gt; &lt;div id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class(); ?&gt;&gt; &lt;h1 class="entry-title"&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?&gt;" rel="bookmark"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt; &lt;div class="entry-meta"&gt; &lt;span class="%1$s"&gt;Posted on&lt;/span&gt; &lt;?php the_date('F jS'); ?&gt; - &lt;a class="comment-link" href="&lt;?php the_permalink(); ?&gt;#comment"&gt;&lt;?php comments_number('Leave a Response!', '1 Response', '% Responses'); ?&gt;&lt;/a&gt; &lt;/div&gt;&lt;!-- .entry-meta --&gt; &lt;br /&gt; &lt;a class="post-thumbnail-thing" href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php echo get_the_post_thumbnail(); ?&gt;&lt;/a&gt; &lt;div class="entry-content"&gt; &lt;?php the_content( __( '&lt;span class="alignright"&gt; &lt;span class="button-css"&gt;Continue Reading &amp;rarr;&lt;/span&gt; &lt;/span&gt;', 'twentyten' ) ); ?&gt;&lt;br /&gt;&lt;hr /&gt; &lt;?php wp_link_pages( array( 'before' =&gt; '&lt;div class="page-link"&gt;' . __( 'Pages:', 'twentyten' ), 'after' =&gt; '&lt;/div&gt;' ) ); ?&gt; &lt;/div&gt;&lt;!-- .entry-content --&gt; &lt;/div&gt;&lt;!-- #post-## --&gt; &lt;?php comments_template( '', true ); ?&gt; &lt;?php endwhile; endif; die(); } </code></pre> <h2>header.php</h2> <pre><code>&lt;?php global $query_string; ?&gt; </code></pre> <p>As I've said, the main problem is $query_string is null :( </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload