Exclude record from findRecords
I've seen a lot of posts that allow you to use findRecords to display one record.
Well, I want to do that in reverse.
I have a pod with a list of categories and their links. Works great but now I've created a category I don't want people to see in the list. So it's kind of silly to list all of the categories in my PHP. Especially if I delete and/or add categories in the future. So what would the PHP code be to exclude a record from my list of records? Some kind of where statement?
10 Answers
So I had a similar need for this functionality that I'll share.
$project2->findRecords('rand()', 4, 'project_type.name = "' . $thecatsa . '" && t.slug != "' . $project_slug . '"');
I was developing a single page for an individual portfolio piece and I wanted to include some functionality to show other Related portfolio projects that were in the same category. However, I did not want the main portfolio project to be shown again in the "Related Projects" section.
I had set the variable $thecatsa to be equal to the project category name, so the query would pull posts from the same category as the portfolio project, but would exclude any post with the same unique slug as the main portfolio project. Boom!
Thanks again to the Pods CMS crew for making such a great and extensible mega-plugin for Wordpress. Keep up the great work!
$where = "category != 'category_name' ";
How about this?
$where = ' t.slug != "basic-recipes"'; $categories->findRecords('name ASC',-1, $where);
You mean like this?: $categories->findRecords('name ASC' $where = "category != 'basic-recipes' ";);
I tried that but got an syntax error.
ah sorry forgot the dot traversal. my example was for this if your wondering:
$where = "category.slug != 'basic-recipes' "; $categories->findRecords('name ASC',-1, $where);
Interesting. I now get a SQL Error as follows:
SQL failed; SQL: SELECT SQL_CALC_FOUND_ROWS DISTINCT t.*, p.id AS pod_id, p.created, p.modified FROM wp_pod p INNER JOIN wp\_pod\_tbl\_recipe\_category t ON t.id = p.tbl_row_id WHERE p.datatype = 4 AND ( category.slug != 'basic-recipes' ) ORDER BY name ASC; Response: Unknown column 'category.slug' in 'where clause'
Yes, slug is the machine name. However, trying to place basic-recipes in double quotes like this:
$where = "category.slug != "basic-recipes" "; $categories->findRecords('name ASC',-1, $where);
Created a fatal error like this: Parse error: syntax error, unexpected T_STRING in /home/thissite/public_html/wp-content/themes/tgsplit/categories.php on line 28
and going like this:
$where = "category.slug != ''basic-recipes'' "; $categories->findRecords('name ASC',-1, $where);
Brought up this:
SQL failed; SQL: SELECT SQL_CALC_FOUND_ROWS DISTINCT t.*, p.id AS pod_id, p.created, p.modified FROM wp_pod p INNER JOIN wp\_pod\_tbl\_recipe\_category t ON t.id = p.tbl_row_id WHERE p.datatype = 4 AND ( category.slug != ''basic-recipes'' ) ORDER BY name ASC; Response: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'basic-recipes'' ) ORDER BY name ASC' at line 11
Try this:
$where = ' category.slug != " basic-recipes " '; $categories->findRecords('name ASC',-1, $where);
I'm still getting the SQL error:
SQL failed; SQL: SELECT SQL_CALC_FOUND_ROWS DISTINCT t.*, p.id AS pod_id, p.created, p.modified FROM wp_pod p INNER JOIN `wp_pod_tbl_recipe_category` t ON t.id = p.tbl_row_id WHERE p.datatype = 4 AND ( category.slug != ''basic-recipes'' ) ORDER BY name ASC; Response: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'basic-recipes'' ) ORDER BY name ASC' at line 11
Just to review, here's how I have my code laid out:
<?php
$recipes = new Pod('recipe');
$category = pods_url_variable(-1);
$where = ' category.slug != " basic-recipes " '; $categories->findRecords('name ASC',-1, $where);
$total_cats = $recipes->getTotalRows();
?>
Yea, I think that finally worked. Thank you, Masino!


