Support » Plugin: Pods - Custom Content Types and Fields » WP Query of PODs

  • Resolved patrickmullins901

    (@patrickmullins901)


    I’m trying to write a WP Query to reference all Posts (standard wordpress ‘post’ type) where my new Custom Post Type (‘people’) is assigned through a one-to-many relationship field (‘content_author’). Think of it like a type of author field for each post, where every post is assigned to one ‘people’. I plan to use this WP Query as a snippet for a custom query in Elementor, since you can’t filter by PODs relationship fields.

    I’m a beginner at PHP, so I’m struggling and was wondering if any of you kind folks could help me write this? Here’s the fields and objects I’m trying to reference using the Magic Tags as a guide.

    Custom Post Type ‘people’
    @post_title
    Kat Gates

    @id
    1313
    Relationship Field extended on post, showing the ID of the two posts associated with this Person
    @authored_content.ID
    3947, 3968`

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    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

    Thread Starter patrickmullins901

    (@patrickmullins901)

    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?
    } );

    Plugin Author Jory Hogeveen

    (@keraweb)

    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

    aefmind

    (@aefmind)

    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.
    Thread Starter patrickmullins901

    (@patrickmullins901)

    @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.

    aefmind

    (@aefmind)

    @patrickmullins901 I just updated my reply for merge it with @keraweb answer.
    You must replace values of “key” and “value”

    Plugin Author Jory Hogeveen

    (@keraweb)

    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

    Thread Starter patrickmullins901

    (@patrickmullins901)

    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
            ),
     ));
    });
    aefmind

    (@aefmind)

    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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.