Tag: WordPress function

HOWTO: Show Page Template Name

A cool function to simply display the path of the WordPress template used on a particular page:

<?php echo get_page_template() ?>

How/when to use this?
If you are trying to debug a site and want to quickly see what page template is used – you can put that PHP code into footer.php and it will then echo out the path so you can quickly and easily see what template is being used.

Using meta_query With WordPress 3.1+

With WordPress 3.1 – we now have the ability to use “meta_query” to show posts associated with a certain custom field. I recently used this in order to create an events listing widget for a client. I needed to query posts in the “Events” category and that had a time stamp defined (which happened to be the start date of the event). The client wanted to show only current/future events (today or later) and wanted to show them in order by the closest event date to the furthest away.

Getting the general query together was a snap – but the orderby did not work unless I had the meta_key defined (this is documented but was overlooked initially).

Example

$now = time();
$args = array(
	'category_name' =&gt; 'Events',
	'meta_query' =&gt; array(
		array(
			'key' =&gt; 'sdac_event_time_stamp',
			'value' =&gt; $now,
			'compare' =&gt; '&gt;=',
			'type' =&gt; 'NUMERIC'
		)
	),
	'meta_key' =&gt; 'sdac_event_time_stamp',
	'order' =&gt; 'ASC',
	'orderby' =&gt; 'meta_value_num'
 );
$events_query = new WP_Query( $args); 

If you take a look at the query itself – the trick was to capture the time now ($now) and then use the compare within the meta_query. Overall – this sort of query makes working with custom fields a lot easier. If you have not checked it out yet – take a look.

Documentation
Function Reference/WP Query

Remove “Private:” From WordPress Titles

I needed to be able filter out the text “Private:” for posts and pages that were password protected but did not want to edit any core WordPress files. To filter out that text, I added the following code into my functions.php file (within my theme directory):

function remove_private_prefix($title) {
$title = str_replace(
'Private:',
'',
$title);
return $title;
}
add_filter('the_title','remove_private_prefix');

(I also posted this filter in the WordPress Support Forum)