Jump to content

[Solved] Problem with featured posts and parents


Joss
 Share

Recommended Posts

Not my parents!

But my categories.

I have a category template into which I call two functions:

getFeaturedPostsbyCategory()

getPostsbyCategory()

The functions are absolutely identical except the featured posts one has this:

$thisCategory = wire("page")->title;

$posts = wire("pages")->find("post_category_select=$thisCategory, post_featured_category=1");

and the other one has this:

$thisCategory = wire("page")->title;

$posts = wire("pages")->find("post_category_select=$thisCategory, post_featured_category=0");

Okay, now the weird stuff!

I have these categories in a tree:

Category 1

-- Category 1 2

---- Category 1 2 3

I create a post and select (through a page single select dropdown) the category "Category 1 2"

If it is not featured, it only appears in the page for Category 1 2

However, If I check the Featured checkbox, it now appears in Category 1 2 AND Category 1!

If I create a post and select category 1 2 3 and make it featured it now appears in category 1 2 3 AND category 1 2 (but NOT category 1)

So,when featured the articles appear in both their own category and the parent category.

WHY? ???

This has me completely stumped.

Joss

==========================

A couple of notes:

This does not happen with posts that are NOT featured (so post_featured_category=0). They just appear in their correct category and do not sneak around with their Category's parent!

The category tree is created as viewable pages in the Pages list. Nothing special about them. Not stored anywhere strange

The posts are all stored under a single hidden page called Posts.

Here are the two functions in full:

<?php
// Get featured posts by category

function getFeaturedPostsbyCategory(){
$thisCategory = wire("page")->title;
$posts = wire("pages")->find("post_category_select=$thisCategory, post_featured_category=1");
$featuredColumns = wire("page")->post_featured_columns;

if($featuredColumns == 2){
    $featuredColumn = "span6";
}elseif ($featuredColumns == 1) {
    $featuredColumn = "span12";
} else {
    $featuredColumn = "span12";
}
    foreach ($posts as $post) {
        $out .="<div class='{$featuredColumn}'>";
        if("$post->primaryimage"){
        $out .="<a href='{$post->primaryimage->url}' class=''>";        
        $out .="<img src='{$post->primaryimage->getThumb("listingthumbnail")}' class='pull-left' />";
        $out .="</a>";
    }
        $out .="<a href='{$post->url}'><h3>{$post->title}</h3></a>";
        $out .="<div class='post-byline'><i class='icon-time'></i> {$post->date} - Written By: {$post->createdUser->user_firstname} {$post->createdUser->user_lastname}</div>";
        $out .=$post->summary;
        $out .="</div>";
    } 
    return $out;
}

// Get posts by category - does not include featured posts

function getPostsbyCategory() {
    $thisCategory = wire("page")->title;
    $postColumns = wire("page")->post_columns;
    if($postColumns == 1){
        $columns = "span12";
    }elseif ($postColumns == 2){
        $columns = "span6";
    }elseif ($postColumns == 3) {
        $columns = "span4";
    }else{
        $columns = "span12";
    }
    $posts = wire("pages")->find("post_category_select=$thisCategory, post_featured_category=0");
    $out = " ";
    
    foreach ($posts as $post) {
        $out .="<div class='{$columns}'>";
        $out .="<h3>{$post->title}</h3>";
        $out .="</div>";
    }
    
    return $out;
}
 ?>
Link to comment
Share on other sites

Hi joss,

Not sure if this is the error, but I think this code is not correct:

$thisCategory = wire("page")->title;
$posts = wire("pages")->find("post_category_select=$thisCategory, post_featured_category=1");

Try:

$thisCategory = wire("page"); //Note: Now it's the page object and not the title
$posts = wire("pages")->find("post_category_select=$thisCategory, post_featured_category=1");

Does it work?

Link to comment
Share on other sites

This is a case of editing far too late at night!

Although my page field was just a single select, on the details tab it was set for multiple - so was saving every change .... DOH!

All working nice and sweetly now.

Link to comment
Share on other sites

Page field can be used with many sort of inputs I think, ids, objects, name... best is you can also use a PageArray too and it will magicly select multiple pages that have at least one of the pages in the array selected.

But title doesn't work here, you're definately doing something wrong elsewhere. If you say both works same, I'd guess you're looking at wrong window :P

It's strange to see you using title to compare, as I think it's one that's one that would not be so save, as when you change title the name field doesn't change and title isn't unique. 

The only way I can reproduce your behaviour is when changing a single page field to page array, then save 2 different entries and change field back to single. This does save two id's in the field db table, page_id and data is the reference. Now there's case it can have two entries in db even the page field should only hold one, then in the code you have it would find both. This can only be corrected when selecting the empty and save the page. Otherwise it will stay there hidden.

Link to comment
Share on other sites

Too late - found the problem. Title was fine, accidentally having the page field set to multiple wasn't :)

Actually, title is not a problem in this particular case. I was going to use ID (and I might still do for consistency), but in fact if I change the title of the category, it is changed for the category select too, so they will always match. 

(I tried it to check)

The problem only comes up if you are actually saving the title somewhere as a string.

Link to comment
Share on other sites

Actually it really doesnt work with title of pages. At least for me. The label displayed has nothing to do with how it's compared i thougth.. it's only what is displayed as the label in the select.

$page->path does work for example. PageArray or Page get converted to pipe separated id string in a selector 1233|1231|3232 which is just saying one of those id's.

Link to comment
Share on other sites

Well, I am too knackered to give it a proper work out - I have it set to ID anyway, because that probably is more sensible.

I now need to make a list of all my various sort options to see if I have gone a bit over the top. 

I need something that is the search equivalent of the lifts in Hitch Hikers guide to the Galaxy.

They had a prescient mode so they could predict what floor you would be waiting at before you knew yourself, so there was always a lift waiting for you when you arrived.

Instead I tend to end up with the idiots video recorder:
 

Records the programme you didn't want to see and then plays it back when you are not there...

Joss

Link to comment
Share on other sites

Oh of course it does work with title if the title is written same as the page name, because name works. Most of the time my title are different from the name, mostly uppercase letters or some more words.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...