Skip to content

Instantly share code, notes, and snippets.

@maor
maor / single-id-template.php
Created December 1, 2012 20:22
Add this to your functions.php or functionality plugin
<?php
function my_single_template_by_post_id( $located_template ) {
return locate_template( array( sprintf( "single-%d.php", absint( get_the_ID() ) ), $located_template ) );
}
add_filter( 'single_template', 'my_single_template_by_post_id' );
@maor
maor / sliced-query.php
Created October 26, 2012 13:39
How WP queries date-based archives
if ( $q['m'] ) {
$q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
$where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
if ( strlen($q['m']) > 5 )
$where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
if ( strlen($q['m']) > 7 )
$where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
if ( strlen($q['m']) > 9 )
$where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
if ( strlen($q['m']) > 11 )
<?php
/* Plugin Name: P2 Force Login
* Description: Force logged-out users to log-in in order to view the P2
* Author: Maor Chasen
* Author URI: http://maorchasen.com/
* Version: 1.0
*/
function p2fl_force_login() {
is_user_logged_in() || auth_redirect();
<?php
/* [http://on.fb.me/UAe3H9] */
/*
* Posts that have the 'mc_order_key' custom field, and that their (numeric) value is low
* will be shown first. If you wish to reverse the order, change the 'order' parameter to
* 'DESC'. Also make sure to change the meta key.
*/
function mc_reorder_search_results( $query ) {
<?php
function maor_twitter_run_wp_user_search_query() {
/* There's a reason we're adding the action from here, you'll see later why. */
add_action( 'pre_user_query', 'maor_twitter_help_main' );
/* Create a new WP_User_Query object, limit recordset to 10 */
$wp_user_search = new WP_User_Query(
array(
'number' => 10
add_action( 'pre_get_posts', 'mc_modify_query_rand_posts' );
function mc_modify_query_rand_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() )
$query->set( 'orderby', 'rand' );
}