Filter blog posts by category
Hi,
If I use:
$CI->fuel_blog->get_category_posts(array('cat1','cat2'))
I will get all the posts that are either in category 'cat1' or category 'cat2'.
Is there a way of easily getting only the posts that figure in both categories?
Comments
$CI->load->module_model(BLOG_FOLDER, 'blog_posts_model'); $CI->blog_posts_model->db()->where('blog_categories.slug', 'cat1'); $CI->blog_posts_model->db()->or_where('blog_categories.slug', 'cat2'); $cat_posts = $CI->blog_posts_model->find_all();
Message: Undefined property: Page_router::$blog_posts
My BLOG module is in "./fuel/modules/blog", default install - no changes. I'm using Fuel 1.2.1.
What am I doing wrong?
$cat_posts = $CI->blog_posts_model->find_all();
But now I'm getting a DB error:
Unknown column 'blog_categories.slug' in 'where clause' SELECT `fuel_blocks`.* FROM (`fuel_blocks`) WHERE `blog_categories`.`slug` = 'cat1' OR `blog_categories`.`slug` = 'cat2' AND `name` = 'commonHeader' AND `language` = 'fr' AND `fuel_blocks`.`published` = 'yes' LIMIT 1
I'm trying to figure out why it is pulling from the fuel_blocks table. Any ideas?
Unknown column 'fuel_blog_categories.slug' in 'where clause' SELECT `fuel_blocks`.* FROM (`fuel_blocks`) WHERE `fuel_blog_categories`.`slug` = 'cat1' OR `fuel_blog_categories`.`slug` = 'cat2' AND `name` = 'commonHeader' AND `language` = 'fr' AND `fuel_blocks`.`published` = 'yes' LIMIT 1
$cat_posts = $CI->blog_posts_model->find_all();
There is a has_many blocks relationship in the blog_posts_model. If you comment that out, what happens?
Also, change any ->db-> to ->db()->:
$CI->blog_posts_model->db()->where('blog_categories.slug', 'cat1'); $CI->blog_posts_model->db()->or_where('blog_categories.slug', 'cat2');
Commenting out the blocks relationship in blog_posts_model has no effect (I had already tried).
Changing db to db()...
$CI->blog_posts_model->db()->where('blog_categories.slug', 'cat1'); $CI->blog_posts_model->db()->or_where('blog_categories.slug', 'cat2');
... does work. However the result is identical to the original ...$CI->fuel_blog->get_category_posts(array('cat1','cat2'))
...since the where clauses are combined using OR.What I'm looking for is combining the where clauses with AND. Here is where it gets sticky again. If I call for only one condition, either cat1 or cat2...
$CI->blog_posts_model->db()->where('blog_categories.slug', 'cat1');
... it works fine.If I call for both using an array...
$CI->blog_posts_model->db()->where(array('fuel_blog_categories.slug'=>'cat1','fuel_blog_categories.slug'=>'cat2') );
...only the last condition is met (here only matches for cat2 will be returned).Now if I call for both sequentially...
$CI->blog_posts_model->db()->where('blog_categories.slug', 'cat1'); $CI->blog_posts_model->db()->where('blog_categories.slug', 'cat2');
...which essentially should be the same as using the array to pass the where conditions, nothing is returned.SELECT fuel_blog_posts.*, fuel_blog_users.display_name, CONCAT(fuel_users.first_name, " ", fuel_users.last_name) as author_name, YEAR(fuel_blog_posts.post_date) as year, DATE_FORMAT(fuel_blog_posts.post_date, "%m") as month, DATE_FORMAT(fuel_blog_posts.post_date, "%d") as day FROM (`fuel_blog_posts`) LEFT JOIN `fuel_relationships` ON `fuel_relationships`.`candidate_key` = `fuel_blog_posts`.`id` AND fuel_relationships.candidate_table = "fuel_blog_posts" AND fuel_relationships.foreign_table = "fuel_blog_categories" LEFT JOIN `fuel_blog_users` ON `fuel_blog_users`.`fuel_user_id` = `fuel_blog_posts`.`author_id` LEFT JOIN `fuel_users` ON `fuel_users`.`id` = `fuel_blog_posts`.`author_id` LEFT JOIN `fuel_blog_categories` ON `fuel_blog_categories`.`id` = `fuel_relationships`.`foreign_key` WHERE `fuel_blog_posts`.`id` = 0 GROUP BY `fuel_blog_posts`.`id` LIMIT 1
It reports this query regardless of which WHERE conditions are used. I'm not sure if I'm using it right. I've inserted $CI->blog_posts_model->debug_query() right before $vars['posts'] = $CI->blog_posts_model->find_all();, in my variables file.
SELECT fuel_blog_posts.*, fuel_blog_users.display_name, CONCAT(fuel_users.first_name, " ", fuel_users.last_name) as author_name, YEAR(fuel_blog_posts.post_date) as year, DATE_FORMAT(fuel_blog_posts.post_date, "%m") as month, DATE_FORMAT(fuel_blog_posts.post_date, "%d") as day FROM (`fuel_blog_posts`) LEFT JOIN `fuel_relationships` ON `fuel_relationships`.`candidate_key` = `fuel_blog_posts`.`id` AND fuel_relationships.candidate_table = "fuel_blog_posts" AND fuel_relationships.foreign_table = "fuel_blog_categories" LEFT JOIN `fuel_blog_users` ON `fuel_blog_users`.`fuel_user_id` = `fuel_blog_posts`.`author_id` LEFT JOIN `fuel_users` ON `fuel_users`.`id` = `fuel_blog_posts`.`author_id` LEFT JOIN `fuel_blog_categories` ON `fuel_blog_categories`.`id` = `fuel_relationships`.`foreign_key` WHERE `fuel_blog_categories`.`slug` = 'cat2' AND `fuel_blog_posts`.`published` = 'yes' GROUP BY `fuel_blog_posts`.`id`
The WHERE clause only contains the last array element (cat2); these are the posts that are returned.
If I specifiy them seperatly, I get this query:
(the first par of the query is identical, so I won't repeat) ...
`fuel_relationships`.`foreign_key` WHERE `fuel_blog_categories`.`slug` = 'cat1' AND `fuel_blog_categories`.`slug` = 'cat2' AND `fuel_blog_posts`.`published` = 'yes' GROUP BY `fuel_blog_posts`.`id`
This one doesn't return anything. Not an expert, but the queries seem to be constructed correctly. I wouldn't know where to look next.