Hi @patrickmullins901
This would be done by using the meta_query
parameter.
Each relationship is stored as a meta value in the database so you can filter/query by this value.
Alternatively, if you just want to query the relationships of the current object, you can use the include
parameter.
Example; show all related posts:
$query = new WP_Query(array(
'include' => pods()->field( 'relationship_field', array( 'output' => 'ids' ) ),
));
Example; Get all posts
that have the current people
object as a relationship:
$query = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'relationship_field_in_post_object',
'value' => get_the_ID(), // The current object ID: <code>people</code> post
),
),
));
I’m not sure what you should return in a Elementor query filter, you’ll have to check this yourself.
Cheers, Jory
Thank you so much, one follow up questions, as I’m still having a little trouble getting it to work. This is how the code snippets for Elementor are structured to you can call them in the posts widget, see my note below in the code for how to place what you wrote above:
`add_action( ‘elementor/query/custom-query’, function( $query ) {
$query->set( ‘post_type’, [ ‘custom-post-type-1’, ‘custom-post-type-2’ ] ); // This is an example query, do I just paste what you’ve provided above in place of this line?
} );
Hi @patrickmullins901
From what I can see, it’s passing a WP_Query object.
See the WP_Object reference on how to add/modify parameters.
https://developer.wordpress.org/reference/classes/wp_query/
Example:
$query->set( 'include', 'LIST OF POST ID's' );
or
$query->set( 'meta_query', 'THE META QUERY ARRAY, SEE PREV COMMENT' );
Cheers, Jory
Hi @keraweb
Elementor Custom Query Filter passes a WP_Query object as parameter for you to modify it.
It’s like in this topic: https://wordpress.org/support/topic/pod-elementor-and-custom-query-filter/
In your second example, it would like this:
add_action( ‘elementor/query/_ID_’, function( $query ) {
$query->set( 'meta_query', array(
array(
'key' => '_REPLACE_IT_FOR_PODS_FIELD_NAME',
'value' => get_the_ID(), // The current object ID
),
);
})
Where _ID_
is the one that is assigned in query id
field in the elementor widget.
But I’ve a doubt. Checking the database, I’ve found that meta_key is sometimes prefixed by ‘_pods_’, and I don’t know what the difference is.
-
This reply was modified 5 months ago by
aefmind.
-
This reply was modified 5 months ago by
aefmind.
-
This reply was modified 5 months ago by
aefmind.
-
This reply was modified 5 months ago by
aefmind.
-
This reply was modified 5 months ago by
aefmind.
@aefmind so if your example, do I need to replace the values of ‘meta_key’ and ‘meta_value’ with something to get this to work? Sorry, I understand HTML/CSS, but PHP is over my head, so I’m struggling with this. Thanks in advance for your help.
@patrickmullins901 I just updated my reply for merge it with @keraweb answer.
You must replace values of “key” and “value”
But I’ve a doubt. Checking the database, I’ve found that meta_key is sometimes prefixed by ‘_pods_’, and I don’t know what the difference is.
This is just for internal use (ordering), you can ignore it for filters like these!
If you use the field()
function of Pods it will automatically handle sorting of the relationship ID’s.
Cheers, Jory
So i’ve updated this to include the name of my relationship field that I’ve extended onto the posts object, is there more to the field name that I’m missing? This isn’t registering as a valid query when referencing it on the front end.
add_action( ‘elementor/query/custom_q’, function( $query ) {
$query->set( 'meta_query', array(
array(
'key' => 'content_author',
'value' => get_the_ID(), // The current object ID
),
));
});
You must replace get_the_ID()
in value
by the id
of that content_author
.
get_the_ID()
returns the id of current post. So currently you are saying: “find all posts where id of content author equals to id of current post”.
But really you want say: “find all posts where id of content author equals to X”.
The way to get the id of content author varies according your use case. You must analyze your case and figure out the best way to get that id.