1

I have some code that I've written to pull attachments in my posts table of my wordpress site.

The first function pulls down the results, but I cannot get it to write to a text file.

Creation of the file is fine, and I get no errors. And the code is operating, I'm just not getting a log. I want to know what files it is prepping to move.

This is the function in question from my plugin

function getPostsToMove($baseurl){  

    $baseurl = $baseurl.'/%/%/%.%'; 
    $myfile = fopen("/websites/site.dev/wp-content/uploads/newfile.txt", "w") or die("Unable to open file!");

    global $wpdb  ;

    return $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE `guid` not 
    like '$baseurl' and `post_type` = 'attachment' ORDER BY post_date DESC LIMIT 1000");

    fwrite($myfile, $baseurl);
    fclose($myfile);
}

What am I missing?

EDIT- I had the order wrong. THose below were right. But apparently I'm not fetching the data from my return. Do I need to turn that into an array?

New code

function getPostsToMove($baseurl){  

$baseurl = $baseurl.'/%/%/%.%'; 
    $myfile = fopen("/websites/site.dev/wp-content/uploads/newfile.txt", "w") or die("Unable to open file!");

    fwrite($myfile, $output);
    fclose($myfile);
    global $wpdb;  
    $output = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE `guid` not 

like '$baseurl' and `post_type` = 'attachment' ORDER BY post_date DESC LIMIT 1000");

return $output;

}
7
  • 3
    return means return... nothing is executed after a return Commented Oct 4, 2016 at 22:12
  • Yes, try with return at the end of the function. Commented Oct 4, 2016 at 22:25
  • This is just the first function that gathers the files to be moved, how can I write and close the file before the query happens? Commented Oct 4, 2016 at 22:36
  • For the record, I tried it, just to be sure. It did not write anything. But the plugin worked. Commented Oct 4, 2016 at 22:37
  • I mean, maybe that's the right order, but I'm not fetching the data that is returned. I don't know. I've not done this before. Commented Oct 4, 2016 at 22:40

1 Answer 1

2

Your function appears to be retrieving the query AFTER attempting to write to the file.

As it looks like you are planning to retrieve the data, write it to a file, and then return the data for further use in your application, I would suggest something like below.

function getPostsToMove($baseurl){  
    $baseurl = $baseurl.'/%/%/%.%';
    $myfile = fopen("/websites/site.dev/wp-content/uploads/newfile.txt", "w") or die("Unable to open file!");

    // Query the data and assign the variable before writing
    global $wpdb;
    $results = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE `guid` not like '$baseurl' and `post_type` = 'attachment' ORDER BY post_date DESC LIMIT 1000");

    // json_encode() to turn the retrieved array into a JSON string for writing to text file
    $output = json_encode($results);

    // write the file
    fwrite($myfile, $output);
    fclose($myfile);

    // return the result (array) for further use in application
    return $results
}
Sign up to request clarification or add additional context in comments.

3 Comments

json_encode() is one way to make the array write to a text file. You could, of course, parse the information into a more readable fashion and write accordingly. But that's outside the scope of this question
THANK YOU!. That is what I was missing. The actual parsing and outputting. I would like to learn the better way if you have a link to or resource on that topic. It would be good to know for the future.
Something like this looks like a good resource to delve deeper into turning an array into 'human readable' format.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.