#14349 closed enhancement (worksforme)
Filter to query_posts() args
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.0 |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description
Will be nice a filter to the query_posts() args and related functions (like get_posts()).
That way, we will can add our own args, like:
query_posts('my_custom_arg=my_special_value&...');
Attachments (1)
Change History (12)
#3
follow-up:
↓ 7
@
15 years ago
- Type changed from feature request to enhancement
Well, the earliest hook is 'parse_query', which might or might not be good enough.
So a nice clean filter right at the beginning of WP_Query::parse_query() wouldn't hurt.
#4
@
15 years ago
Something like:
add_filter('query_args-my_custom_arg', 'parse_my_custom_arg');
/
@param string $arg_value Value of my custom arg
@param array $sql_query That will be a ARRAY with all the sql's (posts_where, joins etc)
/
function parse_my_custom_arg($arg_value, $sql_query){
$sql_querywhere? .= " AND custom_something = '$arg_value' "
do another things
return $sql_query;
}
to:
query_posts('my_custom_arg=arg-value');
I thought something like that =)
Srry for the bad english, I'm brazilian
#5
@
15 years ago
Nice; I would love to have access to all the query bits in one callback.
I would even be happy with this:
function alter_query_bits( $sql_query, $query_vars ) { if ( isset( $query_vars['my_custom_arg'] ) ) { // do some stuff to $sql_query } return $sql_query; } add_filter( 'query_bits', 'alter_query_bits', 10, 2);
#6
@
15 years ago
- Keywords needs-patch added; query_posts filter hooks removed
- Milestone changed from Awaiting Review to 3.1
#7
in reply to:
↑ 3
;
follow-ups:
↓ 8
↓ 10
@
15 years ago
Replying to scribu:
Well, the earliest hook is 'parse_query', which might or might not be good enough.
So a nice clean filter right at the beginning of WP_Query::parse_query() wouldn't hurt.
What would that do for custom query variables that using a callback on parse_query
doesn't? In other words, I don't see anything here that can't easily be accomplished using existing hooks.
#8
in reply to:
↑ 7
;
follow-up:
↓ 9
@
15 years ago
Replying to filosofo:
What would that do for custom query variables that using a callback on
parse_query
doesn't? In other words, I don't see anything here that can't easily be accomplished using existing hooks.
Seconding that.
#9
in reply to:
↑ 8
@
15 years ago
- Keywords needs-patch removed
- Milestone 3.1 deleted
- Resolution set to worksforme
- Status changed from new to closed
Most times, when you have to manipulate the raw SQL request, you need to alter multiple sections in the query. Therefore, you need to register a callback for each SQL segment and repeat the same check in each callback:
function fictional_alter_where( $where, $wp_query ) { if ( $wp_query->get( 'custom_var' ) ) { $where .= " AND meta_value IN ( 'foo', 'bar' )"; } return $where; } add_filter( 'posts_where', 'fictional_alter_where' ); function fictional_alter_join( $join, $wp_query ) { global $wpdb; if ( $wp_query->get( 'custom_var' ) ) { $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id )"; } return $join; } add_filter( 'posts_where', 'fictional_alter_join' );
I think you'll agree that this is pretty cumbersome.
Using wp-query-manipulation.php:
function fictional_alter_query( $bits, $wp_query ) { global $wpdb; if ( $wp_query->get( 'custom_var' ) ) { $bits['where'] .= " AND meta_value IN ( 'foo', 'bar' )"; $bits['join'] .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id )"; } return $bits; } new WP_Query_Manipulation( 'fictional_alter_query' );
I'm so happy I've found an easier way to do this and it doesn't even require a patch. Thanks, lucaswxp :D
#10
in reply to:
↑ 7
@
15 years ago
Replying to filosofo:
Replying to scribu:
Well, the earliest hook is 'parse_query', which might or might not be good enough.
So a nice clean filter right at the beginning of WP_Query::parse_query() wouldn't hurt.
What would that do for custom query variables that using a callback on
parse_query
doesn't? In other words, I don't see anything here that can't easily be accomplished using existing hooks.
It would be useful if you needed to alter the query variables before all the query flags were set. Currently, you would have to set them manually, or call parse_query() again somehow.
I believe there are filters inside WP_Query which should be able to be utilised for this?