• Guys,

    I’m struggling with this and wondering if I am possibly missing something obvious?

    My understanding is the the following query should spit out the posts according to their alphabetical order? The thing is, each catagory is a sub of cat 3. What I want to do is simply present all the posts according to their alphabetical order. But, this dosnt seem to be happening.

    <?php $my_query = new WP_Query(‘cat=3,-34&orderby=title&posts_per_page=500’); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <!– Do special_cat stuff… –>

    The page – ie, result is here… https://www.reviewrecruiter.com.au/reviews

    Am I missing something? Please help, I havnt slept all night on this and I’m about to explode ! ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • Easier to read, not tested but do let me know if it’s working.

    <?php
    $args = array(
     'cat' => array('3', '-34'),
     'orderby' => 'title',
     'posts_per_page' => '-1'
    );
    
    $my_query = new WP_Query($args); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <!-- Do special_cat stuff... -->
    Thread Starter razorharrison

    (@razorharrison)

    Nope. That actually returns posts from ALL categories, including others outside of 3.

    Instead of using “cat” as your parameter, use “post__in”. Take a look at WP_Query() in the codex to see what I mean.

    Also, to get alphabetical order, check out this page. Looks like you want to use “orderby=title”.

    delete:
    'cat' => array('3', '-34'),

    replace with:
    'category__not_in' => array('34'),

    Thread Starter razorharrison

    (@razorharrison)

    Thanks Zeo. I know that both methods should work but believe one of my plugins must be messing up the global variables.

    To resolve this issue, I filtered the results where they spit them out by fetching the parent category and not displaying them if they fall into any other cat.

    If anyone is interested, the code is;

    <?php $category = get_the_category(); ?>
    <?php $parent = get_cat_name($category[0]->category_parent); ?>

    <?php if ($parent == “FOO”) { ?>

    Thread Starter razorharrison

    (@razorharrison)

    Again this issue was not a coding issue but a server memory error.

    Please change to resolved.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WP_Query Problem?’ is closed to new replies.