get_query_var function not working at all

Yes, there are some similar topics about this function,
get_query_var(‘paged’) giving same result

blog posts sorting doesnt work while using get_query_var

I am using wordpress3.5, I have my own php mysql based system, and I need to integrated a blog function , and I just need to list the latest posts on a seprate php file oreder by post-date. Basicly, my requirements is simple:

list all of posts on a php file, suppose this file's name is blog_show.php

Because I will use wordpress’s API, outside wordpress, I need to import wordpress first, you can find the way from here Integration WordPress with your sites

Here is some of my blog_show.php

define('WP_USE_THEMES', false);
require_once '../blog/wp-load.php';

global $wp_query;
wp_reset_query();

$current_page = (get_query_var('p')) ? get_query_var('p') : 1;

$query_args = array(
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 2,
    'paged' => $current_page,
    //'add_args' => true

);
$lateset_posts = new WP_Query($query_args);


$paging_args = array(
    'base'         => '%_%',
    'format'       => '?p=%#%',
    'total'        => 10,
    'current'      => $current_page,
    'end_size'     => 1,
    'mid_size'     => 1,
    'prev_next'    => True,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »')
);
$lateset_posts_paging = paginate_links($paging_args);

Everything is ok, I can use

$lateset_posts = new WP_Query($query_args);

to find out the posts what I want to. But I cannot use

get_query_var('p')

I always get the same value.
And I found even I declare wp_query is gloable, it still cannot be used, almost every fields of wp_query is null.

Becase of get_query_var('p') and $wp_query cannot be used, so my pagnation is also can not be used.

As you see, the var p are only used in two places

$current_page = (get_query_var('p')) ? get_query_var('p') : 1;
$query_args = array(
       //something
       'paged' => $current_page,
    );

and

$paging_args = array(
        'base'         => '%_%',
        'format'       => '?p=%#%', //P's here
        'total'        => 10,
        'current'      => $current_page,
        'end_size'     => 1,
        'mid_size'     => 1,
        'prev_next'    => True,
        'prev_text'    => __('« Previous'),
        'next_text'    => __('Next »')
    );

I have a test, even I changed p to paged or anything else, still get the same error.

Where am I wrong, do I need to require some extra php files ?
Thanks.

1
1

WordPress doesn’t automatically add all query string params ($_GET params) as query_vars.

When Query Var somevar is not registered:
example.com/some-page/?somevar=hello
– WordPress ignores somevar

When Query Var somevar is registered:
example.com/some-page/?somevar=hello
– WordPress stores the value of this param in the $wp_query->query_vars array

The difference between registering that variable with WordPress is whether the value is stored in the WP_Query object.. (it should still be available via $_GET regardless).

To register your custom query var, you should use:

add_filter('query_vars', 'add_my_var');
function add_my_var($public_query_vars) {
    $public_query_vars[] = 'some_unique_identifier_for_your_var';
    return $public_query_vars;
}

Also look out not to use default WordPress query_var names – you’ll probably get some conflicts then, I guess.

Leave a Comment