• I’m wondering if there is a way to pull custom post data from one site we manage to another? I’m not asking about exporting/importing, but rather maintaining a single record that can be accessed from 3 sites?

    Here is the situation… We have a family of 3 salons each with a different website. All three websites are on the same server and use the same custom theme I build… just designed different through CSS.

    We have a custom post type of ‘Talent’ for each of the stylists. Some work at multiple locations. We have a filter on the site to search for and find stylist for any of the 3 locations. ‘Locations’ is a custom taxonomy along with ‘Specialties’ and ‘Experience Level’.

    Currently, we are having to make the same updates to all 3 websites talent which seems redundant.

    Is there a way to maintain a single database of talent and their associated custom taxonomy that can be accessed across all three sites?

    Again I’m not looking for a import/export solution as I have already asked that in a different forum.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi @aaron4osu

    What you can do is connect to the database of another installs and query them via SQL to get the data out.

    $seconddb = new wpdb(USERNAME, PASSWORD, DATABASE_NAME, HOSTNAME);
    Now you can call any wpdb functions on this object, like $seconddb->get_results("SELECT * FROM wp_posts LIMIT 1")

    If you by chance have them in a same database (if you are using different wpdb prefixes for each install), then you simply query them by choosing the right table.

    Thread Starter aaron4osu

    (@aaron4osu)

    Thanks @ashfame ! this might do exactly what I’m looking for. Can I access $seconddb and call the typical wordpress query for custom loops new WP_Query( $args )?

    • This reply was modified 3 years, 4 months ago by aaron4osu.
    • This reply was modified 3 years, 4 months ago by aaron4osu.

    You shouldn’t be using WP_Query in this case because you just need to pull some data out and use it. You don’t really need to set the context/state of the kind of request the install is running in, in this particular request.

    So, just get the data out and use those variables to do whatever.

    Also, I should mention that you can also use WordPress’s REST API to query data from each other install, but since you are probably querying custom data, it would also require you to expose that information on the API, which means extra work. Hence, I didn’t suggest that route.

    Thread Starter aaron4osu

    (@aaron4osu)

    Thanks @ashfame I’m still having issues trying to figure this out. How can I run a query for my custom post type of Talent? and then loop through and get each talent post? Here is what I’m trying to do… the original query line is commented out so you can see what I was doing which works.

    
    // set up second DB
    $DB_NAME2 = '...';
    $DB_USER2 = '...';
    $DB_PASSWORD2 = '...';
    $DB_HOST2 = 'localhost';
    
    $second_db = new wpdb($DB_USER2, $DB_PASSWORD2, $DB_NAME2, $DB_HOST2);
    
    $location = 'locationName';
    
    $args = "";
    
    $args = array( 
      'orderby' => 'title',
      'order' => 'ASC',
      'post_type' => 'the-talent',
      'tax_query' => array(
          array (
              'taxonomy' => 'location',
              'field' => 'slug',
              'terms' => $location,
          )
      ),
      'posts_per_page'=>-1
      );
        
      
      //$talent = new WP_Query( $args );
      $talent = $second_db->get_results($args);
            
        if( $talent->have_posts() ) {
          while( $talent->have_posts() ) {
            
            $talent->the_post();
            $name = get_field('name');
            $bio = get_field('bio');
            $main_image = get_field('main_image');
            
            // do something with variables
    
          }// end while
    
        }// endif
    

    get_results() expects SQL query and not an array of arguments, check – https://developer.www.ads-software.com/reference/classes/wpdb/get_results/

    So, if you just construct the SQL query yourself and supply that to get_results() it will actually get you the data as long as query is right. Try var_dump( $variablename ); to see how the data looks like in it.

    Functions like have_posts(), the_post() etc work on the basis of a global state/context stored as per the main query, which is used while figuring out what content do we need to show on this request – it figures out the context, set certain global variables & then these functions make use of the value of such global variables (state/context).

    For example this piece of code connects to a different database and pulls out the list of tables by running a query:

    
    define('XDB_NAME', 'dbname');
    define('XDB_USER', 'user');
    define('XDB_PASSWORD', 'password');
    define('XDB_HOST', 'localhost');
    
    $wpdb2 = new wpdb( XDB_USER, XDB_PASSWORD, XDB_NAME, XDB_HOST );
    
    $query = "show tables";
    
    print_r( $wpdb2->get_results( $query ) );
    

    Hope that helps.

    Thread Starter aaron4osu

    (@aaron4osu)

    Thanks again, but I’m still having trouble understanding how to loop through the post type with this technique.

    I ran the following code and got an array with all the tables, but what do I do with that?

    
    $wpdb2 = new wpdb( XDB_USER, XDB_PASSWORD, XDB_NAME, XDB_HOST );
    $query = "show tables";
    print_r( $wpdb2->get_results( $query ) );
    

    I also create a mysql query and using print_r it outputted everything on the page

    $talent = “SELECT * FROM wp_posts WHERE post_type = ‘the-talent'”;

    So $talent has all the posts of custom post type talent… but the output using print_r is only showing basic post data and not any of the advanced custom fields data that would normally show when I run the typical post query.

    is there a way to loop through each post inside the $talent variable to get it’s advanced custom fields?… something like this..

    
    if( $talent->have_posts() ) {
          while( $talent->have_posts() ) {
            
            $talent->the_post();
            $name = get_field('name');
            $bio = get_field('bio');
            $main_image = get_field('main_image');
            
            // do something with variables
    
          }// end while
    
        }// endif

    `

    • This reply was modified 3 years, 2 months ago by aaron4osu.
    • This reply was modified 3 years, 2 months ago by aaron4osu.

    So when you call a function get_field(), you don’t need to tell it which post, right? That’s because it knows which post you meant because of state/context/global variables.

    So now when you are working with raw data, all things that were being done for you, you would have to do it yourself. So that would mean making use of get_post_meta() https://developer.www.ads-software.com/reference/functions/get_post_meta/ and passing post ID yourself and similarly with other functions.

    Also filters that were applied on values on any such functions won’t work, as you are simply connecting to its database and not processing the code on the website you are connecting to.

    If this starts becoming too much, you could also explore utilizing REST API to get the data out. https://developer.www.ads-software.com/rest-api/

    Thread Starter aaron4osu

    (@aaron4osu)

    Thanks again @ashfame… unfortunately I think that route is gonna be beyond what I understand…

    So circling back to the original question… just to confirm.. there is no way I can just connect to the the other wp install and write post loops using the typical wordpress functions?

    Yeah, I don’t think that can be done easily. setup_postdata() is a function that can be used to setup global state with the data you have pulled, but that has little benefits, as you will still have to source all the data yourself, which is what you seem to be struggling with.

    The easiest route I think is to just pull all the data with a SQL query like I showed you earlier and then just work with that data. You will need multiple sql queries for fetching their postmeta values as well. Really a play of SQL queries.

    Considering that you are now connected to a different database and are able to make queries, you essentially can access all the data directly, the hardest part is done.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘how to maintain custom post type data for 3 websites?’ is closed to new replies.