Support

Account

Home Forums General Issues Dynamic Select from other plugin table?

Solved

Dynamic Select from other plugin table?

  • Hi Eliot,

    is there any way to get data from a table in my MySQL database related to another plugin?

    My client is going to be creating one or two sliders for each of Posts (I use the RoyalSlider WordPress plugin). I would like them to be able to choose which slider to use for each post, selecting the Title of the slider from a drop-down list on Edit Post page in WP backend.

    Is there an easy way to pull this table data into an ACF Select field for my Posts? Ideally the Titles would show up for the client when the Edit Post, but the field would simply output the id-number of the slider. I can then simple use in my template file (firing RoyalSlider using do_shortcode and this ID number).

    Thanks.

    • Elliot

    • November 13, 2013 at 4:06 pm

    Hi @spacewindow

    This functionality is not part of the ACF plugin. Where is the RoyalSlider data stored? In custom tables in the DB?

    You could use a basic select field, but then hook into the load_field filter, run some custom SQL to find your custom choices, and then update the select field with these values.

    Tutorial: http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/

    Thanks
    E

  • Nailed it. Thanks.

    A mixture of WordPress Class $wpdb (http://codex.wordpress.org/Class_Reference/wpdb) and your functions.

    Saved me (for the moment) from having to write a Save to Database function using WordPress. Also means I can keep my Custom Admin Fields all together in ACF, a bit neater that way.

    RoyalSlider places its data in a table called wp_new_royalsliders (obviously table prefix will be custom to each developers WordPress tables).

    add_filter('acf/load_field/name=media_gallery_slider', 'my_acf_royalslider_choices'); 
    
    function my_acf_royalslider_choices($field){
    
      $field['choices'] = array();
    
      // Required to make use of the wpdb Class
      global $wpdb;
    
      // Query the database
    
      $query = $wpdb->prepare('SELECT * FROM %1$s ORDER BY ID ASC', 'wp_new_royalsliders');
      $results = $wpdb->get_results($query);
    
      // Check for $results
      if(!empty($results)) :
    
      foreach($results as $result) :
    
          $value = $result->id;
          $label = $result->name;
    
          $field['choices'][ $value ] = $label; 
    
      endforeach;
      endif;
      return $field;
    }
    
  • Hi spacewindow

    I’m just using this snippet to return a list of RoyalSliders in a select field for a custom post type. Works perfectly in the backend. How did you get the slider to display on the front end? When using the_field('media_gallery_slider'); I just get the slider ID number displayed. I’m assuming you manage to echo the shortcode on the front end, but can’t figure out how? Any help would be awesome

    Many thanks!

  • Figured it out. Using this:

    <?php $var = get_field('media_gallery_slider');
    if ($var == '')
    { }
    else { echo do_shortcode( '[new_royalslider id="' . $var . '"]'); } ?>
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.