WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple Loops - First loop constrained by second loop (7 posts)

  1. valuxes
    Member
    Posted 4 years ago #

    I have two loops: one in the Header and the standard main one.

    The one in the Header is supposed to retrieve records that match 2 categories for the whole current year. What is happening is that the first loop is only returning records in the matched categories from the 8 records returned by the main loop (main loop set to return only last 8 records).

    Header loop:

    http://wordpress.pastebin.ca/1834779

    The main loop is unchanged.

    I thought that $my_query = new WP_Query would retrieve all records that match cat_1 and cat_2 for the whole year. Instead it returns only for those in the 8 records returned by the main loop. So if the main loop does not contain any records with those 2 categories, the Header loop displays nothing (even if there are lots for this year).

    Thanks for any help.

  2. vtxyzzy
    Member
    Posted 4 years ago #

    I am familiar with 'category__in', but not 'category_name__in'. Are you sure that is a valid parameter? Try using 'category__in' with category IDs in the array.

  3. valuxes
    Member
    Posted 4 years ago #

    Thanks vtxyzzy for the suggestion, it worked!

    The category_name__in used to work in v2.9.1 -- it stopped working when I upgraded to v2.9.2.

    category__in with the IDs works again. But it does not return ALL the posts that match the categories (name or ID) for the year.

    Is the Year test coded correctly? I would like to display all the recs for the current Year or current Year and Month.

  4. vtxyzzy
    Member
    Posted 4 years ago #

    The year looks OK. Maybe you are limited by the number of posts per page. Try setting the posts_per_page in the args like this:

    <?php $my_query = new WP_Query(array(
      'category_name__in'=>array(cat_1,cat_2),
      'year'=>date('Y'),
      'order_by'=>date,
      'order'=>'DESC',
      'posts_per_page' => -1)); ?>
  5. valuxes
    Member
    Posted 4 years ago #

    Perfect!

    I also added a:

    'monthnum'=>date('m') to restrict for the month.

    Thanks so much vtxyzzy!!!

  6. MichaelH
    Member
    Posted 4 years ago #

    Hmm--don't see any reference to category_name__in in core code. Did a plugin provide that?

  7. valuxes
    Member
    Posted 4 years ago #

    MichaelH,

    Not sure. I probably saw category_name used on one of the example pages and added the __in myself (duh!). It "seemed" to work until now. ;)

Topic Closed

This topic has been closed to new replies.

About this Topic