Jump to content
mciccone

Getting categories that have entries with Page Reference field

Recommended Posts

I'm having some trouble with my logic for categories and a page reference field.  I have group of categories (pages) and I have group of entries with a multiple page reference field (an entry can have one or many categories).  I'm trying to create an entries/categories listing page that lists only the categories (once each), as headings, then display the entries for that category below that category heading.   

I can quite easily display all of the categories, even if they don't have entries, or a single category with entries, but I want to loop through dynamically and try to only display categories that actually have entries all on a single page, but having trouble with the page references relation.  I've been going in circles and I think I'm just missing something simple in how I'm approaching it.  My selectors are fairly simple and not much help:

$categories = $pages->find("template=category");
$posts = $pages->find("template=article-post,sort=category");  //category is the page reference field, and it is a multiple select page field

Sorry I'm not at my work computer so I don't have the rest of my code, but it wasn't much help anyways.  Hoping someone has maybe done something similar and can point me in the right direction.

Share this post


Link to post
Share on other sites

Your selectors are not actually doing anything to filter the posts you want:

If for example you have a category called "tech" and you want to find articles with that "category" field, you could do:

$category = $pages->get("template=category, title=tech");
$posts = $pages->find("template=article-post, category=$category");

This also works with filtering multiple categories:

$categories = $pages->find("template=category, title=tech|botany");
$posts = $pages->find("template=article-post, category=$categories");

Now, if you want to do your list with the headings and the posts underneath them, you'd have to loop through your categories and do the same thing. :)

Hope it helps.

  • Like 1

Share this post


Link to post
Share on other sites

What @Alxndre' is suggesting is something like the below...

First you get your category pages, then you loop over the categories, for each category finding pages that have that category selected in the Page Reference field. If there are some matching pages, output the category heading and the list of posts.

$categories = $pages->find("template=category");
foreach($categories as $category) {
    $posts = $pages->find("template=article-post, category=$category");
    if(count($posts)) {
        echo "<h3>$category->title</h3>";
        foreach($posts as $post) {
            // whatever markup you want for the post
            echo "<p>$post->title</p>";
        }
    }
}

This is fine for a lot of circumstances, but note that you do a database query to get the categories and then for each category you do a database query. If you have a lot of categories this would mean a lot of database queries.

There is another approach that is more efficient: get all the posts sorted by category (as you are already), then for each post check if the category is different from the previous post's category. If it is different then output a heading.

$posts = $pages->find("template=article-post, sort=category");
$category_title = '';
foreach($posts as $post) {
    if($post->category->title !== $category_title) {
        $category_title = $post->category->title;
        echo "<h3>$category_title</h3>";
    }
    // whatever markup you want for the post
    echo "<p>$post->title</p>";
}

 

  • Like 4

Share this post


Link to post
Share on other sites

Thanks Robin,

The second one seems to be enough for what I need to do, and the simplest.  I was overcomplicating the situation.  I just had to add another loop to go through the post.category as it's a multiple page reference array.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...