• Resolved Chris Rowley

    (@clubside)


    Howdy,

    I developed a simple page for a friend who does movie reviews to retrieve all his posts in a certain category and display them as a list. All was well until he went over 500 posts with that selected category. This used to work fine:

    <?php $posts = query_posts('cat=3&amp;orderby=title&amp;order=asc&amp;posts_per_page=-1'); ?>

    Now nothing is returned and the rest of the template (that used to show the sidebar and footer) is never processed.

    I have searched for a solution and found a number of suggestions using a custom query, none of those solutions are as simple as needed. For example https://www.ads-software.com/support/topic/259958 shows a custom query for category but it uses the category name and takes tags into account and is therefore tough to modify for what should be an easy query.

    While it would be nice to fix this within the loop, I’d be happy to get some custom query advice, even a pointer to the list of fields in the posts table so I only drew the ones I needed (title, link).

    Thanks!
    Chris

Viewing 2 replies - 1 through 2 (of 2 total)
  • Is it possible PHP is running out of memory to process that query? Check the error log in the WP install folder.

    500 posts is a lot for anyone to visually take in – you might consider dividing them into sub-categories, for example, by name of movie, and display only
    A – E
    F – H
    I – L
    etc at first, revealing the detail when the user clicks on one of the choices. It could be set up so when initially displayed the first sub category is open and displaying. As far as WP, that is much less work it needs to do formatting all those rows for display.

    Thread Starter Chris Rowley

    (@clubside)

    Thanks stvwlf, I hadn’t considered that. Since I only wanted to display the titles like an index I wasn’t counting on all the memory chewed up by the post bodies. I managed to solve the problem myself with additional help from searches so if anyone is interested:

    SELECT DISTINCT p.ID, p.post_title, p.post_name, p.guid, p.post_date
    FROM wp_posts AS p
    	INNER JOIN wp_term_relationships AS tr ON (p.ID = tr.object_id)
    	INNER JOIN wp_term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category' AND tt.term_id IN ( 3 ))
    	LEFT JOIN wp_postmeta AS pm ON(p.ID = pm.post_id)
    WHERE pm.meta_key = 'vern-title-sort'
    ORDER BY pm.meta_value

    That is the code I ended up using which takes advantage of a special field added to sort the posts (so that movies that start with “The” are moved to the proper place). To do the same thing without the special field:

    SELECT DISTINCT p.ID, p.post_title, p.post_name, p.guid, p.post_date
    FROM wp_posts AS p
    	INNER JOIN wp_term_relationships AS tr ON (p.ID = tr.object_id)
    	INNER JOIN wp_term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category' AND tt.term_id IN ( 3 ))
    ORDER BY p.post_title

    This is similar to the example in the Codex but for anyone else wanting to retrieve all posts headers for a certain category (or multiple categories when you change the “IN ( 3 )” to “IN (1, 2, 3)” or whichever numbers you like) there you have it. My final iterative code looks like this:

    <?php
    	$query = "
    		SELECT DISTINCT p.ID, p.post_title, p.post_name, p.guid, p.post_date
    		FROM wp_posts AS p
    			INNER JOIN wp_term_relationships AS tr ON (p.ID = tr.object_id)
    			INNER JOIN wp_term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category' AND tt.term_id IN ( 3 ))
    			LEFT JOIN wp_postmeta AS pm ON(p.ID = pm.post_id)
    		WHERE pm.meta_key = 'vern-title-sort'
    		ORDER BY pm.meta_value";
    	if ( $posts = $wpdb->get_results($query) ) {
    		foreach ($posts as $post) {
    ?>
    	<li><a href="<?php echo get_permalink($post->id)?>"><?php echo $post->post_title ?></a></li>
    <?php
    		}
    	}
    ?>

    Hope this helps someone else!

    Chris

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘500 post limit in Loop’ is closed to new replies.