WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_query - nopaging - how to? (9 posts)

  1. rogersabboes
    Member
    Posted 4 years ago #

    $args=array(
    );
    
    $wp_query = new WP_Query($args);

    Im looking for the right parameters to sets a new WP_Query in that way, that it gets all the posts for the current $query_string. I can't use the global wp_query as it contains only a maximum of 10 posts (the max number is set to 10 in my wordpress read-settings). I guess I need to set 'nopaging' => '1', and stick the new query to the global $query_string. Or am I completely wrong? So far I haven't managed to get it work.
    Any clues?

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Try using posts_per_page' => -1 in your new WP_Query or when using query_posts.

  3. rogersabboes
    Member
    Posted 4 years ago #

    Yes thanks esmi, this one solves the problem partly. I'm getting now all the posts instead of only ten.
    It looks like so:

    $args=array(
    'posts_per_page' => '-1');
    $currentposts = new WP_Query($args);

    But still the query ignores the querystring from the current url. The result of the query is static, it retrieves all the posts all the time, no matter if the current page is for instance http://www.root.net or http://www.root.net?tag=anything .
    So I need somehow make the new WP_Query consider the global $query_string (like for instance: ?tag=anything), just the way like the global wp_query would do it.

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    It might help if you described what you're trying to do.

  5. rogersabboes
    Member
    Posted 4 years ago #

    Actually I want to have a list of categories in the sidebar of my theme. Only those categories should show up in the list which are assigned to the posts shown in the loop.
    So lets say I navigate to main page where all posts show up, the category-list would list all categories. When I navigate to a tag-archive (?tag=anything), the category list should only show those categories which are assigned to the posts in this specific tag-archive. And so on..
    I use this function for it in the sidebar:

    $cat_array = array();
    $args=array(
    'posts_per_page' => '-1');
    $currentposts = new WP_Query($args);
       foreach($currentposts->posts as $currentposts->post) {
        foreach(get_the_category($currentposts->post->ID) as $category) {
          $cat_array[$category->term_id] =  $category->term_id;
        }
      }
    
    $cat_ids = implode(',', $cat_array);
    wp_list_categories('include='.$cat_ids.'&title_li=Author Categories&show_count=1');

    When I use $currentposts = $wp_query; instead of the new WP_query, everything works perfect, except that global $wp_query onyl considers a maximum of 10 posts, as this is the number i set at the WP reading settings.

  6. esmi
    Forum Moderator
    Posted 4 years ago #

    You need to add the current tag to your query arguments:

    http://codex.wordpress.org/Function_Reference/query_posts#Tag_Parameters

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    Merge your args with the existing ones?

    $args = array_merge( array( 'posts_per_page' => '-1' ), $wp_query->query );
  8. rogersabboes
    Member
    Posted 4 years ago #

    This is absolute perfect! It just works when merging the args!

    I think adding a specific tag to to the query arguments would'nt help in my case, because the function is supposed to get the arguments automatically, depending on which site the user navigates to.

    Thanks all a lot for solving my problem!

  9. joehamilton
    Member
    Posted 3 years ago #

    I would like to do the same thing but listing a custom taxonomies instead of categories?
    How would I go about doing this?

Topic Closed

This topic has been closed to new replies.

About this Topic