Support » Themes and Templates » Create New Variable In Post And Sort Category Template By That Variable

  • Resolved gearheaddeals


    I have tried searching and reading a few threads but this level of code is above my current abilities.

    I am trying to setup a category template that will sort posts based on a new variable I within a New Post body.
    The variable is the date that the item sale ends in a format similar to MM-DD-YYYY so when sorted ascending should put the earliest expiration date first.

    Pseudocode would be something like:

    Add New Post–>
    Post content……
    Expires On: [create variable “expiration_date” = input(MM-DD-YYYY)]
    display “expiration_date”
    countdown timer plugin (pass in variable “expiration_date” for use by plugin)



    sort by “expiration_date” order=ASC


    I will be able to post the current template code later from home.

    Can anyone help with this? It seems simple enough, but I am just lost right now.

    Thank you very much.

Viewing 15 replies - 1 through 15 (of 25 total)
  • Having played with something similar a while ago, I’d suggest that you save your expiration dates in the form YYYY-MM-DD as this will make it much easier to run custom custom queries that return posts in the right order. Individual post/sale expiration dates could then be added as custom fields.

    that format is acceptable and I had tried a plugin that was supposed to create a custom text field i named expiration and used the format YYYYMMDD to have a whole number.

    but I really want to create the variable in each post content and not as a field since I need to display the date anyways. dont want to have to type it twice.

    You can’t place PHP variables inside the post content area.


    so then I have to do it twice. no big deal. the plugin I mentioned creates php variables right?

    I guess if the answer is yes then I just need help with the sorting code.

    I’ve not used or examined that plugin, so I couldn’t say either way.

    were you able to sort yours by the php variable you made?

    I used custom fields.

    and the custom field makes a php variable?
    I have a custom field plugin running now.

    could you explain a bit more?

    Have a look at Using_Custom_Fields. That might explain how you can use custom fields better than I can.

    I’ve been trying to use that, but am really a novice at this part of wordpress.

    Here is part of the code where I believe the new code should be inserted:

    <div id="main" class="clear">
    			<div id="content">
    				<?php while (have_posts()) : the_post(); ?>
    					<h1 class="title"><?php the_title(); ?></h1>
    					<div class="entry page">
    						<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail( 'index-thumb', array( 'class' => 'single-post-thm alignright border' ) ); ?>
    						<?php the_content(); ?>
    						<?php edit_post_link(__( 'Edit', 'traction' )); ?>
    						<?php wp_link_pages(); ?>
    					</div><!--end entry-->

    I am looking to use this code snippet but do not know where in there to put it and retain the while loop:

    $querystr = "
    			SELECT wposts.*
    			FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    			WHERE wposts.ID = wpostmeta.post_id
    			AND wpostmeta.meta_key = 'date'
    			AND wposts.post_status = 'publish'
    			AND wposts.post_type = 'post'
    			ORDER BY STR_TO_DATE(wpostmeta.meta_value, '%m/%d/%Y') ASC
    		$pageposts = $wpdb->get_results($querystr, OBJECT);

    Something like query_posts( $query_string . '&meta_key=expiration_date&orderby=meta_value_num'); should pull the posts in the right order.

    thank you, and where is that placed in the first code block?

    Immediately before <?php while (have_posts()) : the_post(); ?>.

    and the date format needs to be 10/05/2011 or 20111005 or something else?

    thank you very much by the way

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Create New Variable In Post And Sort Category Template By That Variable’ is closed to new replies.