• Resolved regme

    (@regme)


    Have some problem with grouping posts in sub_categories after adding argumetns to query_post. Hel me please

    <?
    $catid = get_cat_id( single_cat_title("",false) );;
    
    $args = array(
    	'cat' => '$catid',
    	'post_type' => 'post',
    	'meta_key'  => 'MY_KEY',
    	'orderby'   => 'meta_value_num',
    	'order'      => 'DESC',
    );
    query_posts( $args );
    if ( have_posts() ) :
    	while ( have_posts() ) : the_post();
    		?><a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /><?php
    	endwhile;
    endif;
    wp_reset_query();
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey regme,

    You have a few syntax errors and I’m unsure if your first function is valid, single_cat_title probably isn’t what you’re looking for. Try starting by hardcoding a category ID into your function to be sure it works. You can then replace it with a function once you know the rest of your code operates normally.

    I removed an extra semicolon and an extra comma.

    <?
    $catid = 1; //The Category with ID = 1
    
    $args = array(
    	'cat' => '$catid', //Only show this category
    	'post_type' => 'post', //Only show posts
    	'meta_key'  => 'MY_KEY', //Display posts with a custom field of "MY_KEY"
    	'orderby'   => 'meta_value_num', //Sort by the numeric value of MY_KEY
    	'order'      => 'DESC' //Descending order
    );
    query_posts( $args );
    if ( have_posts() ) :
    	while ( have_posts() ) : the_post();
    		?><a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /><?php
    	endwhile;
    endif;
    wp_reset_query();
    ?>

    As a heads up – I only did a spot check of your code and haven’t been able to test it. I’m not exactly sure how you’re trying to sort so I hope this helps get you started.

    I suggest checking out this resources as well: https://www.billerickson.net/code/wp_query-arguments/

    Cheers!
    -Tyler

    Thread Starter regme

    (@regme)

    Tyler Shadick, thanks for reply! You have helped me figer out the reason.
    This code is good for me^

    $args = array(
    'post_type' => 'post',
    'cat' =>get_query_var('cat'),
    'meta_key'  => 'META_KEY',
    'orderby'   => 'meta_value_num',
    'order'      => 'DESC',
    'posts_per_page' => 10,
    'paged' => $paged,
    );

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problem with sort in sub_categories’ is closed to new replies.