[wp-trac] [WordPress Trac] #18822: get_permalink() produces an add. DB request without $post->filter
WordPress Trac
wp-trac at lists.automattic.com
Fri Sep 30 10:46:02 UTC 2011
#18822: get_permalink() produces an add. DB request without $post->filter
-------------------------+-----------------------------
Reporter: F J Kaiser | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version: 3.2.1
Severity: major | Keywords: needs-patch
-------------------------+-----------------------------
When used in a Query-Loop, `get_permalink()` makes an additional query
`$post = &get_post($id);` to produce a valid link to the post (in example:
archive pages). This means that you'll get one additional query ''per''
post-permalink on your archive page, which can pretty fast sum up.
When looking at [http://core.trac.wordpress.org/browser/trunk/wp-includes
/link-template.php?annotate=blame&rev=18328#L95 link-template.php line
95], you can see the `if`/`else` statement that avoids throwing a complete
`$post` object into the function to produce a permalink without an
additional query.
''The exact line that prevents using the post object:''
{{{
if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
}}}
So far the only possible solutions that can prevent that behaviour are
shown in this [http://wordpress.stackexchange.com/questions/28933/why-
does-get-permalink-produces-an-add-db-request-without-post-filter Q on
WordPress StackExchange] - see ''answer by Scribu'' & ''Edit 3 by
myself''.
I haven't tested this for a patch, but maybe replacing the line with
{{{
if ( is_object($id) XOR (isset($id->filter) && 'sample' === $id->filter) )
{
}}}
would be enough, to make both the test cases/samples work as well as to
prevent the additional query.
As adding a permalink to a post on archive pages is pretty common, I'd say
this could be close to a major perfomance increase for WordPress Loops.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/18822>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list