Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query Posts Problem WordPress

  • SOLVED

I use to_count_archives to call the number of posts per page from my theme options.
My query_posts is below.

The problems are as following, and they need to be fixed:
- (THIS IS THE MOST IMPORTANT) The code should only call posts from the category I'm on. This code is calling all posts on my site.
- The code should fix the pages issue: Let's say I have 10 posts. When I have 5 posts per page selected in theme options and 10 posts in the standard WordPress settings I am show 5 posts per page, but when I hit previous posts get a 404. WordPress is by default generating the number of posts per page set in the WordPress settings.

Here is the code I am using:

<?php
$per_page = get_option('to_count_archives');
query_posts("posts_per_page={$per_page}");
if (have_posts())
?>
<?php while (have_posts()) : the_post(); ?>

Answers (6)

2011-10-08

Jurre Hanema answers:

My advice is to dump the approach using query_posts() and use a filter on the main query instead. So, first you <strong>remove</strong> these lines from your original code:

$per_page = get_option('to_count_archives');
query_posts("posts_per_page={$per_page}");

and then you add this to your functions.php:

add_filter('parse_query', 'wpq_parse_query');

function wpq_parse_query($query)
{
if($query->is_archive())
{
$query->query_vars['posts_per_page'] = get_option('to_count_archives', 10);
}

return $query;
}

This will limit the number of posts per page for all archives to the value of the option to_count_archives, with working pagination.


Lucas Wynne comments:

I think this will work, just a couple questions about this...

- What does the '10' represent in the code?

- If I wanted to use this for my index page and category.php would I just change if($query->is_archive()) to if($query->is_index()) and if($query->is_portfolio())?

- I want to use a similar code on portfolio.php, would this be if($query->is_portfolio())?


Jurre Hanema comments:

The 10 is simply a default value that will be used whenever the option to_count_archives is not set.

is_index() and is_portfolio() are not existing Wordpress functions, so that won't work.

To check if the index page is being shown, you can use the function is_home(). So if you want to apply the code to both archive pages and the index page, write:


if($query->is_archive() || $query->is_home())


As for your portfolio.php I'm not sure because I don't know what it represents on your site. Does it show a custom post type archive or is it a custom page template or something else?


Lucas Wynne comments:

portfolio.php is a custom archive/category page. I am calling posts in it like so:


<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = get_option('to_count_portfolio');
query_posts("posts_per_page=".$per_page."&paged=".$paged."&cat=".$cat);
if (have_posts())
?>
<?php while (have_posts()) : the_post(); ?>



The code you pasted works, HOWEVER, portfolio.php is now using the number of posts called by to_count_archives and not to_count_portfolio as it should. Once I get a remedy for this situation I'll award you the money.


Jurre Hanema comments:

I'm a bit confused by your code. You say that portfolio.php is a custom archive or category page (which are not the same), but that is in fact impossible because then the filename should be archive-portfolio.php (in the case of a custom post type archive) or category-portfolio.php (in the case of a custom category template).

However, in the case of a category (which from your site I guess it is), try this.

- Rename portfolio.php to category-portfolio.php
- Remove these lines from the original code:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = get_option('to_count_portfolio');
query_posts("posts_per_page=".$per_page."&paged=".$paged."&cat=".$cat);

- Modify my original function like this:

function wpq_parse_query($query)
{
if($query->is_category('portfolio'))
{
$query->query_vars['posts_per_page'] = get_option('to_count_portfolio', 10);
} else if($query->is_archive() || $query->is_home())
{
$query->query_vars['posts_per_page'] = get_option('to_count_archives', 10);
}

return $query;
}

2011-10-06

dr_killpatient answers:

Hi there


try this

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = get_option(posts_per_page);
query_posts("posts_per_page={$per_page}");
...


Lucas Wynne comments:

Didn't generate pages correctly.

2011-10-06

Luis Abarca answers:



<?php

$per_page = get_option('to_count_archives');

query_posts( "posts_per_page={$per_page}&paged=" . get_query_var('page') );

if (have_posts())

?>

<?php while (have_posts()) : the_post(); ?>


Lucas Wynne comments:

Didn't generate pages correctly.

2011-10-06

Sébastien | French WordpressDesigner answers:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$per_page = get_option('to_count_archives');

query_posts("posts_per_page=".$per_page."&paged=".$paged."&cat=".$cat);

if (have_posts())

?>

<?php while (have_posts()) : the_post(); ?>


is your code in category.php or archives.php ?


Lucas Wynne comments:

archive.php - This didn't help with the pages.


Sébastien | French WordpressDesigner comments:

so rename your template category.php and upload this file on your ftp


Sébastien | French WordpressDesigner comments:

could you take an url where i can see the problem

2011-10-07

Romel Apuya answers:

try this one out


<?php
$per_page = get_option('to_count_archives');
query_posts('posts_per_page={$per_page}&cat='.get_query_var('cat'));

if (have_posts())
?>

<?php while (have_posts()) : the_post(); ?>


Lucas Wynne comments:

This ignores the number of posts selected in the options panel.


Romel Apuya comments:

try this:
<?php

$per_page = get_option('to_count_archives');

query_posts( array( 'cat' => get_query_var('cat'), 'posts_per_page' => {$per_page} ) );


if (have_posts())

?>

<?php while (have_posts()) : the_post(); ?>


Lucas Wynne comments:

unexpected '{'


Romel Apuya comments:

this one should do it i think.

<?php
$per_page = get_option('to_count_archives');
query_posts('cat='.get_query_var('cat').'&posts_per_page='.{$per_page}.');
if (have_posts()):while (have_posts()) : the_post();
?>

2011-10-07

Habib Rosyad answers:

How about this:

<?php
global $cat;
$per_page = get_option('to_count_archives');

$query = new WP_Query("posts_per_page=$per_page&cat=$cat");

if ($query->have_posts()) {
while ($query->have_posts()) : $query->the_post();
?>
//Your HTML or other PHP code here to format the posts
<?php
endwhile;
} else {
//What you do if no posts available
}
?>


Maybe you should use this code inside category.php instead of archive.php, as archive.php is more "general" than category.php, it used to display more than just a category archive (e.g.: date,year archive). Also if you have a category.php present in your template directory, it will be used instead of archive.php as archive.php is available as fallback if category.php is not present (so the above code will never be used). In case you actually don't have category.php, and of course the category archive will be displayed by archive.php, the code above will work, but for the other type of archive it might become a mess...

Oh, I forgot to mention that query_post does have problem with pagination ([[LINK href="http://bit.ly/oKVfkQ"]]bit.ly/oKVfkQ[[/LINK]]), although some of answers above supposed to be solve it (using get_query_var('paged')). If you still insist using query_post this guide might help you: [[LINK href="http://bit.ly/oCLMT0"]]bit.ly/oCLMT0[[/LINK]]

if the code in custom page template:

global $cat;
$per_page = get_option('to_count_archives');
query_posts( array( 'cat' => $cat, 'paged' => get_query_var('page'), 'posts_per_page' => $per_page) );

otherwise (archive.php,category.php,etc):

global $cat;
$per_page = get_option('to_count_archives');
query_posts( array( 'cat' => $cat, 'paged' => get_query_var('paged'), 'posts_per_page' => $per_page ) );


Lucas Wynne comments:

...none of these work.


Habib Rosyad comments:

Are your site live? If yes, may I take a look at the URL that display where the problems are?


Lucas Wynne comments:

[[LINK href="http://themeforward.com/demo2/category/blog/"]]http://themeforward.com/demo2/category/blog/[[/LINK]]