question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query to find all object with a certain custom field value

See original GitHub issue

Hello,

My request is, I have different Post Types and some of them are linked. ( I have my post type “place” and my post type “news”, one news is linked to a place via a custom field ) On my “Place” page, I’d like to list all the news related to it. What I do for the moment is I created a Helper function, which finds all the news for the place. My worry is I’m doing a query which is too long to execute : mesAnims = CamaleonCms::Site.first.posts.sort_by_field("date-de-debut", "desc").where(post_type: 69) In other words, i find all the news. And then I do a loop to get only the ones for my current item. `lesAnims = []

  mesAnims.each do |anim|

    if !anim.get_fields('bistrot-associe',0)[0].nil? && anim.get_fields('bistrot-associe',0)[0] == @post.id.to_s

        lesAnims.push(anim)

    end

  end` 

I would like to know if I could avoid this loop to directly add a “Where mycustomfieldslug = myvalue” . Thanks a lot,

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
owen2345commented, Aug 1, 2017

custom_field_slug is the DB column name to save the custom_field key. Example:

Cama::PostType.first.posts.includes(:custom_field_values).merge(CamaleonCms::CustomFieldsRelationship.where(custom_field_slug: 'my_slug_for_city', value: 'Paris'))
# Filtering all posts that contains custom field key = "my_slug_for_city" and custom field value is "Paris".

Check image for the structure: screen shot 2017-08-01 at 9 24 45 am

Please bundle update, I released another version with the changes *.11

1reaction
owen2345commented, Aug 1, 2017

Hi @floreasy I improved this part and also I added small docs (sorting, filtering, preloading) here: http://camaleon.tuzitio.com/documentation/category/40756-uncategorized/custom-fields-1.html Please review and let me know if it works well for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Search for a field in all objects - Salesforce Stack Exchange
The easiest way to do this is to open the Developer Console, click the Query Editor tab, check the "Use Tooling API" checkbox,...
Read more >
SOQL query to get custom field in standard object from ...
Hey, I have a Custom object (custom_object__c) with a Lookup field for (Contact) from which I would like to get a field or...
Read more >
Advanced search reference - JQL fields | Jira Software Cloud
Search for issues where a particular custom field has a particular value. You can search by custom field name or custom field ID...
Read more >
Query posts by custom fields - ACF
This article will demonstrate how to retrieve an array of post objects from the database using native WP functions. There are many ways...
Read more >
Find Objects by Custom Field - Alation APIs
This API allows you to find objects in the catalog by their custom fields. Note: Custom fields applied from catalog sets are not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found