Make WordPress Core

Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#14349 closed enhancement (worksforme)

Filter to query_posts() args

Reported by: lucaswxp's profile lucaswxp 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)

wp-query-manipulation.php (1.4 KB) - added by scribu 15 years ago.

Download all attachments as: .zip

Change History (12)

#1 @dd32
15 years ago

I believe there are filters inside WP_Query which should be able to be utilised for this?

#2 @dd32
15 years ago

  • Component changed from General to Query

#3 follow-up: @scribu
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 @lucaswxp
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 @scribu
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 @scribu
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: @filosofo
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: @nacin
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 @scribu
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 @scribu
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.

#11 @scribu
15 years ago

Related: #14997.

Note: See TracTickets for help on using tickets.