Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I found a question about magazine style front pages that queries always only 1 post (or custom_post_types, etc.) from a whole load of different categories, tags or custom taxonomies. I thought about this for a while and came with something like this as a start.

function pre_saved_posts() {
    if (
        ! is_admin()
        OR ! current_user_can( 'edit_posts' )
    )
        return;

    global $post;

    // Option-Name
    $opt_name = 'theme_post_ids';
    // get data from DB
    $db_option_value = get_option( $opt_name );
    // If there's nothing in the DB, create an array, else add the ID to the array of post_IDs
    empty( $db_option_value ) ? $post_ids = array( $post->ID ) : $post_ids .= $post->ID;

    // get posts
    $posts = get_posts( array( 'include' => $post_ids ) );

    // DB-Option @Option-Table updaten or add
    empty( $db_option_value ) ? add_option( $opt_name, $posts ) : update_option( $opt_name, $posts );
}
// Do it when a post is updated
add_action( 'save_post', 'pre_saved_posts', 20 ); 

Would be displayed like this:

$some_posts = get_option( 'theme_post_ids' );
foreach ( $some_posts as $post )
{
    // possible for ex.: $post->ID, $post->post_content, $post->post_title, etc.
}

It's not tested and only an example, but maybe there's someone interested in extending this. I still question myself how much sense it makes, but i could imagine that there's one large multidimensional array that contains subarrays for different requests ex.

$some_posts = get_option( 'theme_post_ids' ); $home_posts = $someposts['home'];

If this would be stored in a global $var, maybe there could be some performance increase.

Ideas/Suggestions/Whatever?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Hi @kaiser:

Consider using the Transients API instead, that's what it is built for.

share|improve this answer
    
ehm... yea. completely forgot about this. never used it :) and i already updated my answer at the forum. thanks –  kaiser Feb 1 '11 at 1:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.