WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] its possible to swicth the wp_query on category.php? (13 posts)

  1. homemrobo
    Member
    Posted 3 years ago #

    I don't get it, why this is not working?

    Once I choose 0 or 1, the order of the loop dont change. I'm trying to put a simple "order by" on the default "category.php" page.

    $neworder = 1; // example
    	if ($neworder == 1){
    			$myargs=array(
    			'orderby' => 'title',
    			'order' => 'DESC',
    		  	'post_type' => 'post',
    		  	'post_status' => 'publish',
    			'cat' => $cat_ID,
    			);
    	}elseif ($neworder == 0){
    			$myargs=array(
    			'orderby' => 'title',
    			'order' => 'ASC',
    			'post_type' => 'post',
    			'post_status' => 'publish',
    			'cat' => $cat_ID,
    			);
    	}else{
    			$myargs=array(
    			'post_type' => 'post',
    			'post_status' => 'publish',
    			'cat' => $cat_ID,
    			);
    	}
    	$myloop = new WP_Query($myargs);
    	while ($myloop->have_posts()) : $myloop->the_post();
            // the loop
            endwhile;
  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    what is the result of your code?
    how is $cat_ID defined?

    in a different approach (totally untested), you could try:

    $neworder = 1;
    global $query_string; // example
    	if ($neworder == 1){
    			$myloop = new WP_Query($query_string . '&orderby=title&order=DESC');
    	}elseif ($neworder == 0){
    			$myloop = new WP_Query($query_string . '&orderby=title&order=ASC');
    	}else{
    			$myloop = new WP_Query($query_string);
    	}
    
    	while ($myloop->have_posts()) : $myloop->the_post();
            // the loop
            endwhile;
  3. homemrobo
    Member
    Posted 3 years ago #

    Hi @alchymtyh, I try it.. but didn't work. I think that there's something to do with reseting the query. Or maybe it's "category.php" default stuff.. I dunno.

    Right now, I'm calling the cat_id with this:

    $cat_obj = $wp_query->get_queried_object();
    $cat_ID = $cat_obj->term_id;
    
    // get new order
    $valor_order = isset($_GET["valor_order"]) ? $_GET["valor_order"] : 0;
    $neworder = $valor_order ? 0 : 1;

    The result is always the same, order by title, this case: N, D, B.

    If I swap $neworder values, $myargs is updated, but the loop bring always same results: N, D, B

    Thanks, I'll keep trying.

  4. homemrobo
    Member
    Posted 3 years ago #

    Finally:

    <?php
    // category.php
    get_header(); 
    
    // get the category ID
    $cat_obj = $wp_query->get_queried_object();
    $cat_ID = $cat_obj->term_id;
    
    // grab which order we want from the link order
    $get_order = isset($_GET["order"]) ? $_GET["order"] : NULL;
    
    // toggle neworder if oldorder is not null
    if (!is_null($get_order)){
    	$neworder = $get_order ? 0 : 1;
    }
    ?>
    
    <!-- this is the magical swap link -->
    <a href="?order=<?php echo $neworder; ?>">Swap order</a>
    
    <?php
    // if get_order is set, we have a new order for this loop, using add_filter
    if (isset($get_order)) {
    	// if neworder is 1, ASC, else DESC
    	if ($neworder == 1){
    		add_filter('posts_orderby', create_function('$a',' return " wp_posts.post_title ASC"; '));
    	}else{
    		add_filter('posts_orderby', create_function('$a',' return " wp_posts.post_title DESC"; '));
    	}
    }
    
    // the category query, yes, paged, please
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string .'&paged='.$paged.'&cat='.$cat_ID);
    
    // the loop
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <!-- Do some ninja tricks here... -->
    
    <?php endwhile; endif; ?>

    Some facts:
    - you gonna get some ugly urls: /page/3/?order=1
    - Didn't reset the query before or after the loop
    - I'm using WP 3.0.1
    - This is the category.php from Twenty Ten 1.1 theme
    - But I'm not using get_template_part or any Twenty Ten functions
    - Works fine with WP-PageNavi 2.73, but you need this code on functions.php:

    add_action( 'parse_query', 'category_posts_per_page', 10, 1 );
    function category_posts_per_page( $q_obj ) {
    	if( is_category() )
    		$q_obj->query_vars['posts_per_page'] = 2; // Change this example value
    }

    For more info about this action, please, read this topic. Thanks @Mark/t31os!

    Some related reading:

    - http://wpquestions.com/question/show/id/883

    - http://mitcho.com/blog/how-to/external-orders-in-wordpress-queries/

    I just want to know why I cannot use a simple tricks (query_posts) on this case?

    If you have ideas and want to share, please share.

  5. Mark / t31os
    Moderator
    Posted 3 years ago #

    Just so i'm sure that i follow what the aim is here.

    You want to provide a sorting method on the category archives(pages/views) using a link.. right?

    I'll test some code for you, but a small note before i report back.. There's no need to be passing the category ID into the query, if it's a category archive(query), that query variable/parameter is already set with the appropriate ID..
    -----
    EDIT: Ok, a few notes..

    Both order and orderby are public query variables, neither of them need to be passed into the query, you can use them with their respective values, asc and desc for order, title, date, etc... for orderby ..

    If you want to use differing values to the ones they support, ie. 0 and 1, etc... you'll need to use different names, else you'll clash with the WordPress query vars which expect given values..

    This code works perfectly fine for me in the category.php file of the twenty ten theme using 3.0.1, and works with setting alternate values for order and orderby (without any need for me to pass parameters into the query).

    My test code:
    http://wordpress.pastebin.com/iLsWQivd

    The only thing my code lacks is some links to sort the results, is that what you're aiming to do though? Simple links for sorting?

    NOTE: Regarding paging, posts_per_page seems to work fine to, with exception to setting a value of 1, which causes a problem (bug i think - other values work fine). However do note, i've not passed any paged value into the test query, but it does page correctly. Interesting to find that when not setting the paged value the results page fine, and perhaps there's another bug there, because i see we were passing this value into the query in the previous thread.

  6. homemrobo
    Member
    Posted 3 years ago #

    Hi Mark, the problem is, I try almost every possible way to get query_posts working and nothing...

    I try to merge, I try other ways I try a similar approach like your code, but I'm not using that get_template_part stuff. I should delete some files of the theme? I remove all functions of that functions.php theme. I delete loop.php and start from scratch. Everything is fine, only this category page that breaks. That code above is my current category page.

    I just want a clean category page and a link to toggle the order of the posts...

    The only way to get this toggle working is using that filter. But in future I'll need to order by custom field too (I'm almost crying right now to make more weird filters for custom fields with posts per page and pagination).

    The problem is on the "category.php" file? I still dont get it... I have to use that get_template_part now?

  7. Mark / t31os
    Moderator
    Posted 3 years ago #

    Is there are a good reason you can't switch the category.php file back to the original code, then work in modifications, such as sorting links?

    The code i've posted above works for me, so in theory it should for you (i'm testing the same theme, on the same version of WordPress)..

    I did nothing more then what i've posted(small code addition) and got the functionality i think you're after (no filters, etc).

    The links for sorting are easy, that i'm not too worried about that (can whip you up something within a few mins), but i'd still be interested to know if the code i posted works for you (again note, you shouldn't need a filter - if you still have that filter in place comment it out).

  8. homemrobo
    Member
    Posted 3 years ago #

    Hi Mark, thanks for your time.. here I go, this works:

    <?php
    // category.php
    get_header();
    
    $sidecat_id =4;
    
    // grab which order we want from the link order
    $get_order = isset($_GET["order"]) ? $_GET["order"] : NULL;
    
    // toggle neworder if oldorder is not null
    if (!is_null($get_order)){
    	$neworder = $get_order ? 0 : 1;
    }
    ?>
    
    <!-- this is the magical swap link -->
    <a href="?order=<?php echo $neworder; ?>">toggle</a>
    
    <?php
    // if get_order is set, we have a new order for this loop, using add_filter
    if (isset($get_order)) {
    	// if neworder is 1, ASC, else DESC
    	if ($neworder == 1){
    		query_posts( array_merge(
    			$wp_query->query,
    			array( 'posts_per_page' => 10)
    		) );
    	}else{
    		query_posts( array_merge(
    			$wp_query->query,
    			array( 'posts_per_page' => 1)
    		) );
    	}
    }
    
    // the category query, yes, paged, please
    //$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    //query_posts($query_string .'&paged='.$paged.'&cat='.$cat_ID);
    
    // the loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    
    <?php endwhile; endif; ?>

    But when I try to swap order of the titles, nothing happens, I still got all posts ordered by date, like default category.php does:

    if (isset($get_order)) {
    	// if neworder is 1, ASC, else DESC
    	if ($neworder == 1){
    		query_posts( array_merge(
    			$wp_query->query,
    			array( 'orderby' => 'title', 'order' => 'asc')
    		) );
    	}else{
    		query_posts( array_merge(
    			$wp_query->query,
    			array( 'orderby' => 'title', 'order' => 'desc')
    		) );
    	}
    }

    Note: I remove this code from functions.php:

    add_action( 'parse_query', 'category_posts_per_page', 10, 1 );
    function category_posts_per_page( $q_obj ) {
    	if( is_category() )
    		$q_obj->query_vars['posts_per_page'] = 2; // Example value
    }

    :(

  9. Mark / t31os
    Moderator
    Posted 3 years ago #

    As i said previously you do not need to pass in the order and orderby parameters.. so this is un-necessary..

    array( 'orderby' => 'title', 'order' => 'desc')

    Just query the page providing the parameters and the query will work itself out..

    Additionally as i also mentioned previously, those two query variables expect particular values, giving them anything but supported values will not work, so you can't have order=1 and order=0, the supported values are asc or desc(lower or uppercase).

    As a test, try this code..

    <?php
    // category.php
    get_header();
    
    $sidecat_id = 4;
    ?>
    
    <!-- Test links -->
    <a href="?order=asc">Order asc</a> |
    <a href="?order=desc">Order desc</a> |
    <a href="?orderby=title">By Title</a> |
    <a href="?orderby=date">By Date</a>
    
    <?php if ( have_posts() ) : ?>
    
    	<?php while ( have_posts() ) : the_post(); ?>
    
    		<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    
    	<?php endwhile; ?>
    
    <?php endif; ?>
  10. homemrobo
    Member
    Posted 3 years ago #

    Hey Mark.. don't work. I always get this order of posts:

    test 2
    order test
    test 1
    z test
    new test

  11. homemrobo
    Member
    Posted 3 years ago #

    How can I check if the problem is on the database Mark? Maybe mysql is not working properly.. I'll try from scratch, again, building a new wp, after that I post here the results.

  12. Mark / t31os
    Moderator
    Posted 3 years ago #

    I can't see there being an issue with the database personally.

    If you're going to rollback/start-over try the test code above, or try this version.. (like the one i posted previously using get_template_part).

    http://wordpress.pastebin.com/vLEB3NM7

    NOTE: Each of the pieces of code i've posted has been tested and works inside the category template(category.php) with the TwentyTen theme (theme has no modifications other than those you see in the code i'm posting).

  13. homemrobo
    Member
    Posted 3 years ago #

    Hey Mark! Thanks dude, your code works fine on a new WP install.. Maybe that database was corrupted, I really don't know. I ended up doing this, now I can toggle the order using a meta_value and works fine:

    <?php
    get_header();
    $cat_obj = $wp_query->get_queried_object();
    $querycatID = $cat_obj->term_id;
    
    //toggle link
    $neworder = ($order == 'asc') ? 'desc' : 'asc';
    ?>
    
    <a href="?order=<?php echo $neworder; ?>">swap order</a>
    
    <?php
    
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	if (isset($order)){
    		query_posts('cat='.$querycatID.'&paged='.$paged.'&meta_key=Valor&orderby=meta_value_num&order='.$order);
    	}else{
    		query_posts('cat='.$querycatID.'&paged='.$paged);
    	}
    
    // the loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    <!-- Do some ninja tricks here... -->
    <?php
    endwhile;
    endif;
    
    get_footer();
    ?>

    Thanks again dude!

Topic Closed

This topic has been closed to new replies.

About this Topic