All pastes #718721 Raw Edit

Untitled

public text v1 · immutable
#718721 ·published 2007-09-28 17:20 UTC
rendered paste body
get-a-post plugin (Plugin URI: http://guff.szub.net/get-a-post) modification to be able to choose how to sort and specify a status for the post/page.
There optionals parameters in the function ($sort='DESC',$status='publish'), but you can change them.
For example, if you want to have an intro post for a category; change is date so it's the older and put its status to DRAFT; and modify the template for the choose category to

	<?php if (have_posts()) : ?>
<!--chopper la présentation presse --!>
		<p></p>
		<?php get_a_post(GETPOST,ASC,NONE); ?>
			<div class="post">
				<div class="entry" id="post-intro">
				<h2><?php the_title(); ?></h2>
					<?php the_content(); ?>
				</div>
			</div>

<?
function get_a_post($id='GETPOST',$sort='DESC',$status='publish') {

	global $post, $tableposts, $wp_version, $wpdb;

	if($wp_version < 1.5)
		$table = $tableposts;
	else
		$table = $wpdb->posts;

	$now = current_time('mysql');
	$name_or_id = '';
	
	if(!$id || 'GETPOST' == $id) {
		if($wp_version >= 2.1)
			$query_suffix = "post_type = 'post'";
	} elseif ('GETPAGE' == $id) {
		if($wp_version < 2.1)
			$query_suffix = "post_status = 'static'";
		else
			$query_suffix = "post_type = 'page'";
	} elseif ('GETSTICKY' == $id) {
		if($wp_version < 1.5)
			$table .= ', ' . $tablepostmeta;
		else
			$table .= ', ' . $wpdb->postmeta;
		$query_suffix = "ID = post_id AND meta_key = 'sticky' AND meta_value = 1";
	} else {
		$query_suffix = "(post_status = '".$status."' OR post_status = 'static')";

		if(is_numeric($id)) {
			$name_or_id = "ID = '$id' AND";
		} else {
			$name_or_id = "post_name = '$id' AND";
		}
	}
	if ($status != 'NONE') {
		$query_suffix .= "  AND post_status = '".$status."'";
	}
	
	$post = $wpdb->get_row("SELECT * FROM $table WHERE $name_or_id post_date <= '$now' AND $query_suffix ORDER BY post_date $sort LIMIT 1");
	get_post_custom($post->ID);

	if($wp_version < 1.5)
		start_wp();
	else
		setup_postdata($post);
}
?>