WordPress.org

Forums

[resolved] Create New Variable In Post And Sort Category Template By That Variable (26 posts)

  1. gearheaddeals
    Member
    Posted 3 years ago #

    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)

    -------------------

    category-expire.php

    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.

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    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.

  3. gearheaddeals
    Member
    Posted 3 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 3 years ago #

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

  5. gearheaddeals
    Member
    Posted 3 years ago #

    ok.

    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.

  6. esmi
    Forum Moderator
    Posted 3 years ago #

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

  7. gearheaddeals
    Member
    Posted 3 years ago #

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

  8. esmi
    Forum Moderator
    Posted 3 years ago #

    I used custom fields.

  9. gearheaddeals
    Member
    Posted 3 years ago #

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

    could you explain a bit more?

  10. esmi
    Forum Moderator
    Posted 3 years ago #

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

  11. gearheaddeals
    Member
    Posted 3 years ago #

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

  12. gearheaddeals
    Member
    Posted 3 years ago #

    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);
  13. esmi
    Forum Moderator
    Posted 3 years ago #

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

  14. gearheaddeals
    Member
    Posted 3 years ago #

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

  15. esmi
    Forum Moderator
    Posted 3 years ago #

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

  16. gearheaddeals
    Member
    Posted 3 years ago #

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

    thank you very much by the way

  17. gearheaddeals
    Member
    Posted 3 years ago #

    when I insert your snippet, it just shows up on the top of the category page above the first post. is there another location I should try?

  18. esmi
    Forum Moderator
    Posted 3 years ago #

    Try <?php query_posts( $query_string . '&meta_key=expiration_date&orderby=meta_value_num');?> :-)

  19. gearheaddeals
    Member
    Posted 3 years ago #

    [Code moderated as per the Forum Rules. Please use the pastebin]

    So that's what I have, and this is what I get

    http://www.gearheaddeals.com/category/expiring/

    The exp. date shown there is unrelated to the meta_key. It is just written in the post, but the meta keys match that date and you can see that it is not in order.

    I also changed the meta_key variable name to "expiration" for simplicity

  20. gearheaddeals
    Member
    Posted 3 years ago #

    currently the key is like this 2011-10-05 and the plugin "more fields" is set to take in a date of that format.

  21. gearheaddeals
    Member
    Posted 3 years ago #

    Does anyone have an idea on what I'm doing wrong here?

  22. gearheaddeals
    Member
    Posted 3 years ago #

    I tried changing the field to take the number date in the form
    20111005
    20111006

    and sort ascending but it still isnt working.

    I put this before the loop:

    <?php if (is_category('category-expiring')):
    		    query_posts( $query_string . '&meta_key=expiration&orderby=meta_value_num&order=ASC');
    		    endif; ?>

    still nothing.

  23. gearheaddeals
    Member
    Posted 3 years ago #

    actually replaced the query with some text "test" to make sure it would put "test" above the posts on that category page and got this:

    Parse error: syntax error, unexpected T_ENDIF in /home/foxst2/public_html/filepath.../archive.php on line 27

    But putting the word "test" outside the IF and before the loop, i see what I expected.

  24. gearheaddeals
    Member
    Posted 3 years ago #

    ok somehow I got it working. I copied my entire code from my regular template into my new "category-expiring.php" template, then added the code you had given before

    <?php query_posts( $query_string . '&meta_key=expiration_date&orderby=meta_value_num');?>

    and somehow it worked this time.

    thank you very much.

  25. gearheaddeals
    Member
    Posted 3 years ago #

    now I would like to use an IF statement so in conjunction with the sorting it would be something like:

    IF expiration_date - todays_date > 0
    display post
    ELSE
    END

    so that expired deals would not be posted, even though their checkbox for this category is checked

    any ideas on this one?

  26. gearheaddeals
    Member
    Posted 3 years ago #

    anyone have any help on adding the IF statement just above this post?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.