WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#14349 closed enhancement (worksforme)

Filter to query_posts() args

Reported by: 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 5 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 @dd325 years ago

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

comment:2 @dd325 years ago

  • Component changed from General to Query

comment:3 follow-up: @scribu5 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.

comment:4 @lucaswxp5 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

comment:5 @scribu5 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);

comment:6 @scribu5 years ago

  • Keywords needs-patch added; query_posts filter hooks removed
  • Milestone changed from Awaiting Review to 3.1

comment:7 in reply to: ↑ 3 ; follow-ups: @filosofo5 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.

comment:8 in reply to: ↑ 7 ; follow-up: @nacin5 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.

comment:9 in reply to: ↑ 8 @scribu5 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

comment:10 in reply to: ↑ 7 @scribu5 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.

comment:11 @scribu5 years ago

Related: #14997.

Note: See TracTickets for help on using tickets.