• Resolved ttyd0

    (@ttyd0)


    Hello!
    I’ve got (for now) four categories, one of them has to be sort by custom field (it’s numeric – deadline date without dots, for example 20091225).
    I use “WP Smart Sort” plugin now, but it has some bugs, so i want to solve this by using default functions…
    Problem is – i’m not a programmer =(

    Could you help me?

Viewing 13 replies - 1 through 13 (of 13 total)
  • Do you have a file called category.php in your theme folder:

    wp-content/themes/theme-name/category.php

    If so open this, if not open archive.php..

    Then find a line like this towards the top:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    and before this add something like:

    <?php if(is_category('9')){
    query_posts('meta_key=keyname&orderby=meta_value&order=ASC');
    }
    ?>

    Change 9 to the category id or category name
    Change keyname to the name of the meta key

    If you are still struggling post the top part of the file up.

    Try something like:

    <?php
    if( is_category('9') ) {
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args= array(
    		'meta_key' => 'deadline_date',
    		'orderby' => 'meta_value',
    		'cat' => $thecat,
    		'paged' => $paged
    	);
    	query_posts($args);
    }
    ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    in category php where 9 = the id of your special category.

    Thread Starter ttyd0

    (@ttyd0)

    Nothing happens =(

    I’ve got only archive.php (my theme based on default one)…

    get_header(); ?>
    
    <div class="mainDIV">
    	<div class="left">
    <?php get_sidebar(); ?>
    	</div>
    	<div class="right">
    <?php if(is_category('3')){
    query_posts('meta_key=sort_num&orderby=meta_value&order=ASC');
    }
    ?>
    <?php if (have_posts()) : ?>
    
    	<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
    	<?php /* If this is a category archive */ if (is_category()) { ?>
    <h1><?php printf(__('%s', 'artvladivostok'), single_cat_title('', false)); ?></h1>
    	<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
    <h1><?php printf(__('Posts Tagged ‘%s’', 'artvladivostok'), single_tag_title('', false) ); ?></h1>
    	<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
    <h1><?php printf(_c('Archive for %s|Daily archive page', 'artvladivostok'), get_the_time(__('F jS, Y', 'artvladivostok'))); ?></h1>
    	<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
    <h1><?php printf(_c('Archive for %s|Monthly archive page', 'artvladivostok'), get_the_time(__('F, Y', 'artvladivostok'))); ?></h1>
    	<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
    <h1><?php printf(_c('Archive for %s|Yearly archive page', 'artvladivostok'), get_the_time(__('Y', 'artvladivostok'))); ?></h1>
    	<?php /* If this is an author archive */ } elseif (is_author()) { ?>
    <h1><?php _e('Author Archive', 'artvladivostok'); ?></h1>
    	<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
    <h1><?php _e('Blog Archives', 'artvladivostok'); ?></h1>
    	<?php } ?>
    
    			<?php while (have_posts()) : the_post(); ?>

    Thread Starter ttyd0

    (@ttyd0)

    xdesi, esmi, thank you.
    xdesi, sorry, you code works, my fault, didn’t wait file to be uploaded…

    But there is one thing, if I add custom field in other’s category post – it’ll apears in this one…
    so i see
    cat3post
    cat3post
    cat2post
    cat3post
    in cat3 page.
    there is no big problem with it, i’ll be careful =)

    thanks again.

    But there is one thing, if I add custom field in other’s category post – it’ll apears in this one…

    Change the query_posts line to:

    query_posts('cat=3&meta_key=sort_num&orderby=meta_value&order=ASC');
    Thread Starter ttyd0

    (@ttyd0)

    xdesi, thanks! it works! =)

    Thread Starter ttyd0

    (@ttyd0)

    xdesi, hello.
    i’ve just move this solution to real system…
    there are 6 pages with 10 posts on each, and your code sort posts correctly, but (!) then i try to view second, fird and other pages…
    “wp-pagenavi” plugin show that i stay at first page, system show (at url bar) “…/page/6/” =( and show me first ten posts…

    so i’ve got only ten first posts on each page…

    (there are no any problems on other categories)

    P.S. one more thing.. it’s strange, but esmi code and your first code stop works on real system… both WP versions 2.8.6 now (i’ve just upgrade real site)… both systems has same plugins…

    Did you have pages on your non-real system? And it was working then?

    Anyhow I think esmi’s code looks as though it probably handles the paging issue, as it seems to be setting something in relation to pages, i’m not too sure how it works so hopefully he can advise on this!

    Have you tried using that code?

    Thread Starter ttyd0

    (@ttyd0)

    I’ve got only 4 posts on test system and just make preference for 3 posts per page, and got same result =( with your code.

    Tried to use esmi’s code on test system, it works, but add posts from other categories (that’s no big problem).

    It’s strange, but esmi’s code and yours first cod do not works on real system =( i do not know what is wrong…

    Does this work:

    <?php
    if( is_category('3') ) {
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args= array(
    		'meta_key' => 'sort_num',
    		'orderby' => 'meta_value',
    		'cat' => 3,
    		'paged' => $paged
    	);
    	query_posts($args);
    }
    ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    Thread Starter ttyd0

    (@ttyd0)

    Yes!
    Thank you very much!

    If you merge the original query with your new args (preserving any originally queried for vars), you’ll avoid the need to add bits in (like paging).

    <?php
    // If there's a query for cat 3 (so $wp_query->query, will contain cat=3, or equivalent already if this is true)
    if( is_category('3') ) {
    	// Check if $wp_query is available, only globalise if required
    	if(!$wp_query) {
    		global $wp_query;
    	}
    	// What args you "need to set" (everything else is just read as normal from $wp_query->query)
    	$args = array(
    		'meta_key' => 'sort_num',
    		'orderby' => 'meta_value'
    	);
    	// The final query, your "required args" , plus any that already existed in the query
    	query_posts( array_merge( $args , $wp_query->query ) );
    }
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    Example can also be seen here.
    http://codex.wordpress.org/Template_Tags/query_posts#Preserving_the_Original_Query

    EDIT: Removed paged and cat from args, in this context they should already be inside $wp_query->query … so you don’t need to add them.

    guys, this is great…

    how could i change this code

    <?php
    // If there's a query for cat 3 (so $wp_query->query, will contain cat=3, or equivalent already if this is true)
    if( is_category('3') ) {
    	// Check if $wp_query is available, only globalise if required
    	if(!$wp_query) {
    		global $wp_query;
    	}
    	// What args you "need to set" (everything else is just read as normal from $wp_query->query)
    	$args = array(
    		'meta_key' => 'sort_num',
    		'orderby' => 'meta_value'
    	);
    	// The final query, your "required args" , plus any that already existed in the query
    	query_posts( array_merge( $args , $wp_query->query ) );
    }
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    to be so by default posts are sorted by custom field, and then I can put in the seperate categories ID’s to display by date?

    much appreciated!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Sort posts by custom field in one category (other have to be sort by default)’ is closed to new replies.