3

Scenario: I am creating collapsible panels in the sidebar and displaying ALL categories as DIVs and all Posts links in those category DIVs. Simple enough. I got all categories by get_categories('orderby=name&order=ASC') and now looping through every category to get ALL posts in each category.

Problem: The code get_posts('nopaging=1&category='.$category->cat_ID) gives me all posts but WITH content and other things too. All, I want is the permalink of the post. That's it. I believe using get_posts will thus create big overhead as there are around 1000 posts in over 20 categories.

Solution: A function or piece of code that can just get me all the posts in a category with permalink and without other stuff, especially content as it's the largest overhead.

WhatIsOpenID
  • 1,071
  • 4
  • 12
  • 20

2 Answers2

4

Look into using a custom query to extract exactly what you want: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

You may want to also use wp_reset_query() after you've finished with your custom call because selecting a query can be a fun Gotcha in wordpress when things start overriding each other: http://codex.wordpress.org/Function_Reference/wp_reset_query

Edited after your comment: You'd need to be using a join between wp_term_relationships and wp_posts, possibly even a 3-way join with wp_term_taxonomy thrown in for good measure.

To be absolutely honest, I wouldn't even bother - it's a lot of work for not a lot of benefit. 1000 records isn't really that much, and Wordpress already uses indexes on wp_posts to optimise its' queries. You'd probably get much better results through using the wp super cache plugin to generate static HTML files for visitors ( http://wordpress.org/extend/plugins/wp-super-cache/ ) than by duplicating core WP queries with a handful of columns removed from the select.

hollsk
  • 3,124
  • 24
  • 34
  • Thanks. Can you please tell me what query should I write ? `SELECT ID, post_title FROM posts WHERE ....... `. What should be the where clause for category, i.e. `where category_id = 1` . What should be there ? – WhatIsOpenID Nov 12 '10 at 15:36
  • 1
    I've updated my answer because after some reflection I don't think this approach is a very good one. It's certainly possible, but it's excessively complex and I don't think the benefits compensate for that complexity. – hollsk Nov 12 '10 at 16:09
0

You already tried to use:

the_permalink()  

http://codex.wordpress.org/Function_Reference/the_permalink

get_permalink( $id )    

http://codex.wordpress.org/Function_Reference/get_permalink

Vera
  • 391
  • 1
  • 4
  • 9