Capture query string in Wordpress

Asked

Viewed 34 times

0

I’m trying to capture the query string paged on the main page to call a component and load the data. However, its return is always 0.

When I use this component on other pages, it works normally.

URL structure with Query String

http://minha_url.com.br/estilo/page/2/

Component capturing the query string

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;

Main page calling the component

<section class="col-lg-8 col-xl-9 order-1 order-lg-2">
    <h2 class="sr-only">Posts recentes</h2>
    <?php get_template_part('components/content', 'posts'); ?>
</section>

Return of get_query_var('paged') inside the main page

0

I don’t usually use Wordpress but I believe it is not a complex mistake.

1 answer

1


I believe that 0 is the actual paged value. You can print the global $wp_query->query_vars that the value on the homepage should actually be 0.

I did this with my Wordpress site on the home page and the return was as follows:

Array
(
    [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
    [meta_key] => 
    [meta_value] => 
    [preview] => 
    [s] => 
    [sentence] => 
    [title] => 
    [fields] => 
    [menu_order] => 
    [embed] => 
    [category__in] => Array
        (
        )

    [category__not_in] => Array
        (
        )

    [category__and] => Array
        (
        )

    [post__in] => Array
        (
        )

    [post__not_in] => Array
        (
        )

    [post_name__in] => Array
        (
        )

    [tag__in] => Array
        (
        )

    [tag__not_in] => Array
        (
        )

    [tag__and] => Array
        (
        )

    [tag_slug__in] => Array
        (
        )

    [tag_slug__and] => Array
        (
        )

    [post_parent__in] => Array
        (
        )

    [post_parent__not_in] => Array
        (
        )

    [author__in] => Array
        (
        )

    [author__not_in] => Array
        (
        )

    [ignore_sticky_posts] => 
    [suppress_filters] => 
    [cache_results] => 1
    [update_post_term_cache] => 1
    [lazy_load_term_meta] => 1
    [update_post_meta_cache] => 1
    [post_type] => 
    [posts_per_page] => 10
    [nopaging] => 
    [comments_per_page] => 50
    [no_found_rows] => 
    [order] => DESC
)

The code I used was this:

global $wp_query;
print_r($wp_query->query_vars);
  • But in my case I would have to return the value that is in the URL, it is not?

  • So the value before being in the URL is in the query. But depending on how you set up your query, paging can behave like a static page. In this case you would have to take the value of page instead of paged. Example: $page = (get_query_var('page')) ? get_query_var('page') : 1;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.