We've written a custom Wordpress API and the API has parameter called 'id'. The values that are passed into this parameter could be either Custom IDs defined in a custom field or (if that is empty) a Wordpress Post IDs. In other query languages I could accomplish this several different ways (either two WHERE clauses connected with an OR statement or else with a custom field that is populated with data from either column). I cannot, however, figure out how to do this in Wordpress. I feel like it should be possible, but I can't find any example.
If I was searching for two values in a Meta Query or a Tax Query, I could use an OR statement, but I can't figure out how to do this between a Meta Query and a Non-Meta Query.
Is this possible and how would you do it?
Related
We are implementing a custom REST endpoint in magnolia that queries for pages and it should be able to filter by categories. I found the JCR Query Cheat Sheet, but I could not see an example about filtering a multivalued field. In our case, we have a categories field with an array of category UUIDs and I want all pages having a certain category uuid. It works with using a like query (lets say 123-456 is a uuid) such as:
"SELECT * FROM [mgnl:page] p where p.[categories] like '%123-456%'
Is there a better approach without using (possible slow) LIKE queries to explicitly check for an intersection with the categories array? Are there any SET/ARRAY functions to use in where conditions for such filtering?
In case of multifield values, you can put multiple WHERE statements like so:
SELECT * FROM [mgnl:content] AS p
WHERE p.[categories] = '415025c6-e4b5-4506-9384-34f428a52104'
AND p.[categories] = 'e007e401-1bf8-4658-8293-b9c743784264'
This will return nodes in which categories (multivalue) property contains both IDs.
I'm trying to avoid creating additional custom field for one of the posts, and was wondering if there is a way to query WP DB to retrieve the necessary data.
I have 3 types of posts: Events, Programs and Locations. On the event post type, there is a custom field for program it is related to. On the program post type, there is a custom field for location.
I'm looking for a query, that can add to the single-location.php document, which will retrieve all the events in that location, without the need to create an additional custom field.
My logic was: get me all the events and filter all the events, where the related program, has a related location, which is the title of this post.
Would it be possible?
I've created a Custom Post Type in WordPress named "projects" and now I'm trying to query a single project using the slug as an identifier. In other words, I want the idType to be the SLUG, not ID. I tried the following query but it does not work.
However, this query works (it's a common query that is using the ID) :
The query above is not what i'm looking for, but at least i know that my CPT is registered correctly and that my data can be fetched somehow.
Why I think this is a problem?
Because I used the same type of query for querying WordPress posts (not custom posts types, just posts) and it works fine. I'm being returned the correct data.
Any help is appreciated ;) I don't know if i'm missing something with my query, or if custom post types don't work the same way that posts do.
WPGraphQL version: 1.3.10 Other WordPress plugins installed: Advanced
Custom Fields, Custom Post Type UI
Set the idType to URI. Here's an example, querying a page (it's the same for other post types as well):
NOTE: the GraphiQL IDE has some nice auto-completions and drop-downs to show the available options:
My understanding is the idType:SLUG option is not available when your custom post type is hierarchical since the slug would not be a unique identifier at that point.
Currently, I'm using tablepress to output different info using a table format. I want users to be able to add to existing information. I need a form in wordpress that saves user posts to these different tables. How should I go about this? Sorry if I sound stupid but I'm soft on html.
Thanks.
If you want to insert in posts table using custom form, you can use wp_insert_post().
This function inserts posts (and pages) in the database. It sanitizes variables, does some checks, fills in missing variables like date/time, etc. It takes an object as its argument and returns the post ID of the created post (or 0 if there is an error).
Reference : http://codex.wordpress.org/Function_Reference/wp_insert_post
Nodes with cck checkboxes
need counting nodes based on cck data and displaing via views field
In drupal6 + taxonomy - there is a simple and fast function taxonomy_term_count_nodes()
But I`m thinking about d7 without taxonomy via cck custom field
Are there any API functions for counting nodes based on CCK fileds ?
I don't know if there is an API function to count nodes with CCK fields in D7, but there's nothing magical about API functions, you can easily create the function yourself if you need it. I don't know how the table structure is going to be, but if it looks like CCK in D6, you could do something like this:
function mymodule_field_node_count($content_type, $field_name) {
return db_result(db_query("SELECT COUNT(*) FROM {field_%s}
WHERE %s <> NULL AND %s <> '';",
$content_type, $field_name, $field_name));
}
You could probably make it prettier than the above, this is just to show that is you need to do something, you can just create a function to solve it for you. After all many API functions is often little more than a bit of logic and some queries that's commonly needed.