Support

Account

Home Forums General Issues Display SQL Query When Executed

Helping

Display SQL Query When Executed

  • Hi everyone.

    This is my first time to write an custom SQL Query to retrieve values from WordPress ACF.

    I had read through some article and also ACF Tutorial.
    Querying the database for repeater sub field values

    Although I had obtain the correct value from this SQL Query.

    $wpdb->get_results(
        $wpdb->prepare(
            "SELECT DISTINCT meta_value FROM wp_spc_postmeta WHERE meta_key LIKE %s",
             'pump_price_list_%', 
             'pump_%'
        );
    );

    But that’s not the result what I am looking for.

    Array
    (
        [0] => stdClass Object
            (
                [meta_value] => LEVO 92
            )
    
        [1] => stdClass Object
            (
                [meta_value] => 2.5
            )
    
        [2] => stdClass Object
            (
                [meta_value] => LEVO 95
            )
    
        [3] => stdClass Object
            (
                [meta_value] => 3
            )
    
    )

    My expected result should be something like this.

    Array
    (
        [0] => stdClass Object
            (
                [pump_type] => LEVO 92
            )
    
        [1] => stdClass Object
            (
                [pump_price] => 2.5
            )
    
        [2] => stdClass Object
            (
                [pump_type] => LEVO 95
            )
    
        [3] => stdClass Object
            (
                [meta_price] => 3
            )
    
    )

    Therefore, I’m looking for a way to display the SQL query when ACF is trying to obtain the correct values from database.

    Thanks.

  • Hi @weehong

    There’s actually an easier way to do this using wp_query and the posts_where filter.

    You can find an example here:
    http://www.advancedcustomfields.com/resources/query-posts-custom-fields/
    under the headline “4. Sub custom field values”

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Display SQL Query When Executed’ is closed to new replies.