Skip to content

Instantly share code, notes, and snippets.

@markjaquith
Created June 30, 2011 20:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markjaquith/1057123 to your computer and use it in GitHub Desktop.
Save markjaquith/1057123 to your computer and use it in GitHub Desktop.
WordPress plugin example code to correctly set a custom number of posts to display per page, for specific types of views
<?php
/*
Plugin Name: CWS Posts Per Page
Version: 0.1
Description: Sets a custom number of posts to display per page, for specific types of views
Author: Mark Jaquith
Author URI: http://coveredwebservices.com/
*/
class CWS_Posts_Per_Page_Plugin {
public static $instance;
public $posts_per_page = array(
'day' => 9999,
'month' => 30,
'search' => 30,
'year' => 30,
'author' => 30,
'category' => 30,
);
public $ran = false;
public function __construct() {
self::$instance = $this;
// We use parse_request, because that only runs on primary WP queries. We use that to add the parse_query filter
add_action( 'parse_request', array( $this, 'parse_request' ) );
}
public function parse_request() {
add_action( 'parse_query', array( $this, 'parse_query' ) );
}
public function parse_query( $q ) {
if ( $this->ran )
return $q;
foreach ( $this->posts_per_page as $flag => $ppp ) {
$flag = 'is_' . $flag;
if ( $q->$flag ) {
$q->set( 'posts_per_page', $ppp );
break;
}
}
// If you want to add more logic, do it here
// Mark this code as having run, so it doesn't run more than once
$this->ran = true;
return $q;
}
}
new CWS_Posts_Per_Page_Plugin;
@scribu
Copy link

scribu commented Jun 30, 2011

This line:

remove_action( 'parse_query', array( $this, 'parse_query' ) );

can cause problems, due to this bug:

http://core.trac.wordpress.org/ticket/9968

Alternative approach, using the 'request' filter:

http://wordpress.stackexchange.com/questions/21341/alternative-to-query-posts-for-main-loop/21378#21378

@markjaquith
Copy link
Author

Argh, that's annoying.

Doesn't your solution cause needless overhead, because it runs WP_Query::parse_query() twice?

I guess I could just set a flag that'd prevent it from running again. Gonna try that.

@scribu
Copy link

scribu commented Jun 30, 2011

Well, parse_query() is pretty light, computationally wise, so I'm not too worried about the overhead.

@scribu
Copy link

scribu commented Jul 1, 2011

I still don't get the self::$instance = $this pattern.

Do you prefer to write:

CWS_Posts_Per_Page_Plugin::$instance->some_method()

instead of

CWS_Posts_Per_Page_Plugin::some_method()

?

Or is there some case where you would replace CWS_Posts_Per_Page_Plugin::$instance with something else? That wouldn't really help, since you use $this as the callback for hooks.

@markjaquith
Copy link
Author

I still don't get the self::$instance = $this pattern.

It's so that other people can access the singleton object and remove its filters, without having to have yet another global variable hanging around. The code is completely encapsulated within the class.

CWS_Posts_Per_Page_Plugin::$instance->some_method()

instead of

CWS_Posts_Per_Page_Plugin::some_method()

Those two are not equivalent. The former calls a method on an object. The latter calls a method on a class. If you used the latter, PHP would throw an error when it encountered $this. This is a singleton object that has been instantiated. We want to access that object, its methods, and its properties.

@scribu
Copy link

scribu commented Jul 1, 2011

If you used the latter, PHP would throw an error when it encountered $this.

Yeah, so you could replace $this with self or __CLASS__ and make all the properties and methods static.

@markjaquith
Copy link
Author

Yeah, so you could replace $this with self or __CLASS__ and make all the properties and methods static.

Ah. Just a syntactic preference. self::$foo looks uglier and less familiar than $this->foo to me.

@scribu
Copy link

scribu commented Jul 1, 2011

Can't argue with that. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment