Support » Fixing WordPress » Multiple Loops – First loop constrained by second loop

  • Resolved valuxes

    (@valuxes)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • vtxyzzy

    (@vtxyzzy)

    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.

    valuxes

    (@valuxes)

    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.

    vtxyzzy

    (@vtxyzzy)

    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)); ?>
    valuxes

    (@valuxes)

    Perfect!

    I also added a:

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

    Thanks so much vtxyzzy!!!

    MichaelH

    (@michaelh)

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

    valuxes

    (@valuxes)

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multiple Loops – First loop constrained by second loop’ is closed to new replies.