1

I have two custom post type Author and Book. The book cpt have an ACF Object Field (authors_object_field) that is related to authors. On the author single page i want to display all books that have the author listed in their ACF Object Field. I'm using Elementor as page builder. I used this code but dont seems to work. Need your lights :)

function my_books_by_author_query($query)
{
    $myCurrentAuthorID = get_the_ID();
    $meta_query = [
        [
            'key' => 'authors_object_field',
            'value' => $myCurrentAuthorID,
            'compare' => '==',
        ]
    ];
    $query->set('meta_query', $meta_query);
    $query->set('post_type', 'book');
}
add_action('elementor/query/booksByAuthor', 'my_books_by_author_query');

2 Answers 2

0

Reference Link: https://developers.elementor.com/custom-query-filter/#Using_the_Custom_Filter

arbitrary_name_query_id - the QUERY ID that you fill in the Elementor field,

the code to be placed within functions.php:

add_action( 'elementor/query/arbitrary_name_query_id', function( $query ) {
    $meta_query = $query->get( 'meta_query' ); 
    $meta_query[] = [ 
        'key' => 'acf_key', // put ACF relationship field name 
        'value' => get_the_ID(), 
        'compare' => '=', 
    ]; 
    $query->set( 'meta_query', $meta_query );
    $query->set( 'post_type', 'post_type' ); // put post type name 
});
1
  • Thank you for your answer, My problem is that I use a ACF Post Object Field instead of a ACF relationship field. and I fail to make a filter based on the ID of this Object
    – AKEAmazan
    Sep 14, 2022 at 9:09
0

I solve my problem with this.

function my_books_by_author_query( $query ) {
    $query->set( 'post_type', 'book' );
    $myCurrentAuthorID = get_the_ID();
    $meta_query = [];
    $meta_query[] = [ 
        'key' => 'authors_object_field',
        'value' => $myCurrentAuthorID, 
        'compare' => 'LIKE', 
    ]; 
    $query->set( 'meta_query', $meta_query );
}
add_action( 'elementor/query/booksByAuthor','my_books_by_author_query' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.