简体   繁体   中英

Wordpress Pods: Find on Boolean Field

I have a simple pod with a boolean called featured. I'm trying to use the find() method (using the pods() shortcut) to list all pod entries that have the featured boolean set to true on a page template but I can't get it to work. I found this answer but it's for the older version and doesn't seem to work in Pods 2.0. If there's a simpler way to do this please inform me what I'm trying something like this:

$params = array(
    'orderby' => 'date DESC',
    'where' => 'case_study.has_page = 1',
    'limit' => -1
);
$pods = pods( 'case-study', $params );

The error I receive is:

WordPress database error: [Unknown column 'case_study.has_page' in 'where clause']
SELECT DISTINCT 't'.* FROM 'wp_posts' AS 't' LEFT JOIN 'wp_postmeta' AS 'case_study' ON 'case_study'.'meta_key' = 'case_study' AND 'case_study'.'post_id' = 't'.'id' WHERE 'case_study'.'has_page' = 1 AND 't'.'post_type' = "case_study" ORDER BY 't'.'date' DESC, 't'.'menu_order', 't'.'post_title', 't'.'post_date'

Database Error; SQL: SELECT DISTINCT 't'.* FROM 'wp_posts' AS 't' LEFT JOIN 'wp_postmeta' AS 'case_study' ON 'case_study'.'meta_key' = 'case_study' AND 'case_study'.'post_id' = 't'.'id' WHERE 'case_study'.'has_page' = 1 AND 't'.'post_type' = "case_study" ORDER BY 't'.'date' DESC, 't'.'menu_order', 't'.'post_title', 't'.'post_date'; Response: Unknown column 'case_study.has_page' in 'where clause'

It appears that it's not JOINing/WHEREing to the wp_postmeta correctlyy but I can't quite figure out how to use the find params to do so. It should should be 'case_study'.'meta_key'='has_page' amongst other problems. I've tried making the field a simple relationship with a Yes/No option but still no luck.

Thank you for any help you can provide! It's much appreciated.

如果您的关系字段名为“ has_page”,并且是简单的(自定义),则需要使用以下代码:

has_field.meta_value = 1

Found it! When editing the pod I went to the 'Advanced' settings tab and under 'Capability Type' I set it to 'Custom' from 'Posts.' Voila, it now works! I suppose this setting tells the find calls how to operate on the tables. My final find params were as follows:

$params = array(
    'orderby' => 'date DESC',
    'where' => 'has_page.meta_value = 1',
    'limit'   => -1
);
$casestudies = pods( 'case_study', $params );

Thank you so much Scott for being quick and an awesome help!

Final solution:

My original content type was an extension of post which doesn't seem to play well with relationship fields, at least basic ones, since it appears to assume they generate their own table which they don't always do. As in this case where, since it was a simple custom relationship, the data was all stored in the wp_postmeta table.

The best solution I found was to rebuild my pod as an Advanced Content Type. This way, the pod generates a table all to its own so checking booleans can be done with a simple 'where' => 'has_page' = 1 instead of all the relationship complexity. Thanks for all your help Scott.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM