Make WordPress Core

Opened 10 years ago

Closed 8 years ago

#19041 closed enhancement (wontfix)

function to list all (true) conditionals (in an array)

Reported by: F J Kaiser Owned by: F J Kaiser
Milestone: Priority: normal
Severity: normal Version: 3.3
Component: Query Keywords: has-patch
Focuses: Cc:


Often we need to check against a lot of conditionals. It would be handy to have one "mother" function to retrieve those query conditionals that are set true with a single call.

The attached patch introduces a new function get_conditionals(); that returns a numerical indexed array containing all object parts of $GLOBALS['wp_query']; that start with is_ and are set to true.

Attachments (1)

19041.patch (910 bytes) - added by F J Kaiser 10 years ago.
replaced patch

Download all attachments as: .zip

Change History (9)

#1 @F J Kaiser
10 years ago

Local performance testing show that the needed time is between 0.00003 and 0.00013 seconds.

@F J Kaiser
10 years ago

replaced patch

#2 follow-up: @johnbillion
10 years ago

Can you post a code snippet where use of this function saves code?

#3 in reply to: ↑ 2 @F J Kaiser
10 years ago

Replying to johnbillion:

Can you post a code snippet where use of this function saves code?

I currently use it in two classes (breadcrumbs & pagination) for function routing/name guessing. It lets me avoid long if/elseif/else statements and long switch( $case ) statements.

$output = array();
foreach ( get_conditionals() as $conditional )
    // Can catch everything using a simple string replace:
    $fn_name = str_replace( 'is_', 'get_', $conditional );
    if ( method_exists( __CLASS__, $fn_name ) )
        $output[] = $this->$fn_name( $args );

If someone is not so much into consistant naming, it wouldn't help too much. I'm sure there are other use cases that I'm currently not aware of, but it's a public API extension that doesn't hurt if you don't use it.

#4 @F J Kaiser
10 years ago

  • Owner set to F J Kaiser
  • Status changed from new to assigned

#5 @scribu
9 years ago

On the one hand, I also have a function that does something very similar, so I know that this would be useful at least to some people. On the other hand, it's pretty easy to implement outside of Core.

There are a few problems with the patch:

  • it only works on the main WP_Query instance; it should be a WP_Query method instead.
  • it uses preg_match(), when a simple 'is_' == substr( $key, 0, 3 ) would do.
  • it casts WP_Query to an array, instead of using get_object_vars().

#6 follow-up: @scribu
9 years ago

For reference, here's how I do it:

// Returns an array containing the flags that have been set
function get_query_flags( $wp_query = null ) {
	if ( !$wp_query )
		$wp_query = $GLOBALS['wp_query'];

	$flags = array();

	foreach ( get_object_vars( $wp_query ) as $key => $val ) {
		if ( 'is_' == substr( $key, 0, 3 ) && $val )
			$flags[] = substr( $key, 3 );

	return $flags;

#7 in reply to: ↑ 6 @F J Kaiser
9 years ago

Replying to scribu:

For reference, here's how I do it:

In short: Smart solution.

#8 @wonderboymusic
8 years ago

  • Keywords 2nd-opinion removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from assigned to closed

I don't see any need for this to be in core, you've both already solved your own problem - if this would clean up or refactor a piece of core, cool

Note: See TracTickets for help on using tickets.