ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Better way to query for custom field

July 23, 2009 5:16pm

Subscribe [2]
  • #1 / Jul 23, 2009 5:16pm

    scottystang

    106 posts

    Hey everyone,

    I’m using the query module to query the “apps_weblog” to get the entries that have something entered for the custom field called “external_url”, which happens to be the column field_id_32 in the table exp_weblog_data.  However, the custom field is a different column name on our production server so I’d have to change the query and have two versions, one for dev and one for prod.

    What I want to do is query for the custom field by using it’s name so I don’t have to rely on a column that has a different id based on the order it was created.

    Is there a better way to query for this?  Here’s my query below.

    {exp:query sql="SELECT   count(exp_category_posts.entry_id) as apps_post_count, exp_category_posts.cat_id, exp_weblog_titles.title, exp_weblog_titles.weblog_id, 
                                         exp_categories.cat_url_title, exp_weblogs.blog_name, exp_weblog_data.field_id_32, exp_weblog_data.entry_id
    FROM     exp_category_posts, exp_weblog_titles, exp_categories, exp_weblogs, exp_weblog_data
    WHERE    exp_category_posts.entry_id = exp_weblog_titles.entry_id 
    AND      exp_category_posts.cat_id = exp_categories.cat_id 
    AND      exp_weblog_titles.weblog_id = exp_weblogs.weblog_id 
    AND      exp_weblog_titles.entry_id = exp_weblog_data.entry_id
    AND      exp_weblog_data.field_id_32 != ''
    AND      exp_categories.cat_url_title = '{segment_3}' 
    AND      exp_weblogs.blog_name = 'apps_weblog' 
    GROUP BY exp_category_posts.cat_id"}

    Thanks in advance.

  • #2 / Jul 23, 2009 5:19pm

    Ingmar

    29245 posts

    I’m using the query module to query the “apps_weblog” to get the entries that have something entered for the custom field called “external_url”

    What about this:

    {exp:weblog:entries weblog="apps_weblog" search:external_url="not IS_EMPTY" ...}
  • #3 / Jul 23, 2009 5:20pm

    scottystang

    106 posts

    Cool, didn’t realize a parameter could filter the query for you.  I’ll give that a try, thanks!

    Edit: Oops, thought that was in the query tag not the weblog entries tag.

    I’m trying to determine ahead of time if there are matching entries in the apps_weblog, specifically with a URL in the external_url custom field.  If there are no records, then I do not display a section of a page.

    The problem is I need to know before I enter the weblog:entries tag if there are results since I’m displaying some header text outside of the query.

  • #4 / Jul 23, 2009 5:33pm

    scottystang

    106 posts

    Here’s a more complete view of what I’m trying to do.  I used the search:external_url=“IS_EMPTY” as you suggested and that removed an if block, thanks.

    
    
    

    Note that I’m only displaying the records if the query variable tools_post_count is at least 1.  The problem is that on our dev server the custom field is field_id_32 and our production server it’s field_id_30.  Instead of field id, I’d like to refer to “external_url” so I can have 1 file that works on both dev and prod.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases