Support

Account

Home Forums ACF PRO How to query posts filtered by custom field values from external database

Solving

How to query posts filtered by custom field values from external database

  • Hi,

    I need to query a database for a list of posts based on a custom field value. I have found a solution about that here

    But how can I get these permalinks from an external database?

    I made the DB connection but I cant figure out how to combine the connection with the above’s link code.

  • If you’re connecting to a remote database, you’ll either have to write a custom SQL query or create a new wpdb instance (unsure of the latter).

  • Hi @soko

    Thanks for the post.

    Yes this could be achieved by SQL, you would need something like this:

    <?php
    $wpdbtest_otherdb = new wpdb('username', 'password', 'shops', 'websitesql.whatever.com');
    $wpdbtest_otherdb->show_errors();
    $myshops = $wpdbtest_otherdb->get_results("SELECT * FROM shops");
    foreach ($myshops as $myshop) {
    echo $myshop->shopName . '<br />';
    }
    ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How to query posts filtered by custom field values from external database’ is closed to new replies.