WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query Problem? (7 posts)

  1. razor.harrison
    Member
    Posted 4 years ago #

    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... http://www.reviewrecruiter.com.au/reviews

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

  2. Safirul Alredha
    Member
    Posted 4 years ago #

    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... -->
  3. razor.harrison
    Member
    Posted 4 years ago #

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

  4. Jeremy Pry
    Member
    Posted 4 years ago #

    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".

  5. Safirul Alredha
    Member
    Posted 4 years ago #

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

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

  6. razor.harrison
    Member
    Posted 4 years ago #

    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") { ?>

  7. razor.harrison
    Member
    Posted 4 years ago #

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

    Please change to resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic