Wordpress | Bulk move content from main editor to custom field - php

I've used the Advanced Custom Fields plugin to create a new Custom Field in Wordpress. My aim now is to move all Download Links from "the_content", to the newly created Custom Field "the_field('download_link')". The issue is that I have over 10,000 posts to modify. I was wondering if there is a quick way of doing this, rather than manually moving the download link for each post?
Please see the images below for an idea of what I am trying to achieve.
Before | After
One hurdle is that all content is saved in the "wp_posts" table, where the custom field content is saved in the "wp_postmeta" table.
The content saved in the "download_link" custom field, looks like this in the "wp_postmeta" table:
(8214, 2282, 'download_link', '<div class=\"source\"><img src=\"https://www.google.com/image.png\"></div>'),
(8215, 2282, '_download_link', 'field_5cffd35335ce3'),
(8220, 2280, 'download_link', '<div class=\"source\"><img src=\"https://www.google.com/image.png\"></div>'),
(8221, 2280, '_download_link', 'field_5cffd35335ce3'),
(8226, 2278, 'download_link', '<div class=\"source\"><img src=\"https://www.google.com/image.png\"></div>'),
(8227, 2278, '_download_link', 'field_5cffd35335ce3'),
Can this be done at all? Or is the only real way to achieve this is by moving the download links manually?
Thanks in advance for your help.

It can be done automatically if the format of your download link is always the same.
Backup your database
Create a php file named my-cleanup.php.
Loop over all products, grab the link from the description and move it to a post_meta.
Launch the file with WP-CLI:
wp eval-file my-cleanup.php
Sample code
// Select the posts you want
$args = array(
'post_type' => 'post',
'post_status' => 'any',
// -1 means all posts
// For debug, set this to a small number
'posts_per_page' => 3,
);
// Retrieve posts
$query = new WP_Query( $args );
$posts = $query->posts;
// Pattern to match
// [wpdm_package id='90228']
$pattern = "/(\[wpdm_package id='\d+'\])/";
// Process posts one by one
foreach ( $posts as $post ) {
echo "Processing " . $post->post_title . "\n";
$post_args['ID'] = $post->ID;
// Get the shortcode from the post content
preg_match( $pattern, $post->post_content, $matches );
// Do we have a match?
if ( count( $matches) > 0 ) {
// Retrieve shortcode
$shortcode = $matches[0];
// Convert shortcode, maybe add some HTML
$link = do_shortcode( $shortcode );
// remove shortcode from description
$post_args['post_content'] = preg_replace( $pattern, '', $post->post_content );
// Update post
wp_update_post( $post_args );
// Create the post_metas
add_post_meta( $post->ID, '_download_link', 'field_5cffd35335ce3', true );
add_post_meta( $post->ID, 'download_link', $link, true );
}
}
Notes
Backup your database
Test on a few posts to get confidence in your code. You can restore the previous revision in the editor.
Then try on 100, then 1000. You will often find small differences in the way posts are entered.
This might take hours to run on 10.000 posts, which is why you need a WP-CLI command.

Related

How to achieve search in code for dynamic-created text using PHP code as shortcode in WordPress?

I need to move certain articles on my website, all of them, to another unique category. I am using specific phrases which are at bottom of every article, but after code execution, only two articles are moved to a new category, not all of them.
Also when I search through all posts using the native Dashboard-All posts option, it returns only those two articles, not all of them. I assume that is because the text is not stored in the database because it shows only when the post renders itself on the front end.
How can I include dynamically created text in my code?
My code, just for reference:
<?php
// Move to the category Breaking News.
$target_category_id = 1982;
// Get all posts that contain the search phrase from Syndication.
$posts = get_posts(
array(
's' => 'and has been published here with permission',
'post_status' => 'any',
'posts_per_page' => -1, // This will retrieve all posts.
)
);
// Loop through the posts and move them to the target category.
foreach ( $posts as $post ) {
// Remove the post from any existing categories.
$current_categories = wp_get_post_categories( $post->ID );
wp_remove_object_terms( $post->ID, $current_categories, 'category' );
// Add the post to the target category.
wp_set_post_categories( $post->ID, array( $target_category_id ), true );
}
That specific phrase is created dynamically because I am using the WP Broadcast plugin to add text at the bottom of every broadcasted article, but I am not sure if that phrase is stored in a database or that text is dynamically populated (created) every time post is rendered on the frontend.
This is the code I am using to generate a message (and has been published here with permission) at the bottom of every article:
<?php
function broadcasted_from() {
// Check that Broadcast is enabled.
if ( ! function_exists( 'ThreeWP_Broadcast' ) ) {
return;
}
// Load the broadcast data for this post.
global $post;
$broadcast_data = ThreeWP_Broadcast()->get_post_broadcast_data( get_current_blog_id(), $post->ID );
// This post must be a child. Check for a parent.
$parent = $broadcast_data->get_linked_parent();
if ( ! $parent ) {
return;
}
// Fetch the permalink
switch_to_blog( $parent['blog_id'] );
$blog_name = get_bloginfo( 'name' );
$permalink = get_post_permalink( $parent['post_id'] );
restore_current_blog();
// And now assemble a text.
$r = sprintf( 'This article appeared in %s and has been published here with permission.', $permalink, $blog_name );
return $r;
}
add_shortcode( 'broadcasted_from', 'broadcasted_from' );
add_filter(
'the_content',
function( $content ) {
// Get the broadcast from the text.
$sc_text = do_shortcode( '[broadcasted_from]' );
// Add the text to the content.
$content .= $sc_text;
// Return the expanded content.
return $content;
}
);

How to show post title that I am creating in the content editor?

Is this possible in Wordpress? I am trying to use a pre-defined template for my contents. To do that, I use something like this:
add_filter( 'default_content', 'custom_editor_content' );
function custom_editor_content( $content ) {
$args = array(
'posts_per_page'=> 15,
'orderby' => array(
'ID' => 'DESC' ,
),
);
$query = new WP_Query( $args );
$query_contents=Array();
while ( $query->have_posts() ) {
$query->the_post();
array_push($query_contents,Array(
"id"=>get_the_ID(),
"title"=>get_the_title(),
"url"=>get_permalink(),
));
}
$content = '
'.get_the_title( $id ).'
';
return $content;
}
But I can't get the post title (the one I am creating at that moment), somehow. Does someone know how to do this? If I put the while statement into $content, it shows the whole query in the editor and that's not what I want of course.
All I want to do is to fetch the post title and show it in the content editor (after posting or before, that wouldn't matter)
Could someone help me out?
I think that at the time of execution of this script (which is before the page loads) the post you are "creating" doesn't exists yet and therefore you cannot fetch its title.
You can try following, as the default_content supports second argument, which is the edited post:
add_filter( 'default_content', 'custom_editor_content' );
function custom_editor_content( $content, $post ) {
...
$content = '
'.$post->post_title.'
';
return $content;
}
However I think you will be facing the same issue as mentioned at the beginning of my answer. If you want to add title to content for new post, you will probably need a javascript which will copy the title into content as you write it (only if the content is still empty).

How to Push WordPress Post Content via Plugin Update?

We have an in-house WordPress plugin that creates content using a custom-post-type. Essentially, the content created is, for all intents and purposes, identical to WordPress 'Pages'.
The idea is that we add these custom 'pages' - via our plugin - and then deploy/update them for whomever is using the plugin. We've used our plugin to create a series of these pages, but the question is how to port this custom content with the plugin.
Initially we were thinking that since our posts have a custom post-type, we can easily identify them in the DB. In fact, we use that to remove our custom content when uninstalling the plugin.
But how do we do the inverse? Ideally we'd like to update the plugin, and with it, any custom content that we've added or modified.
Originally we were thinking of just using MySQL scripts to add this custom post content, but after a bit of research, this doesn't seem like the right way to do it.
I know WordPress has a wp_insert_post() function, but I'm just not sure of how it all fits together.
Ideally, the answer would be an overview of the process for updating our custom content. For instance, should there be a function in our plugin that, upon installation, looks for a sql file and creates new posts from it?
Thanks!
If I understand this correctly and you are trying to keep content sync'ed across multiple installations via plugin updates you will need to create a unique ID for all of your content. When you do updates programatically you are updating by the post ID but each installation will have different post IDs. You could use a post meta field to give each post a unique ID specific to your plugin. You can then query the posts based on your unique meta field.
I would code the plugin so it checks to see if there is a post with your unique ID. If it is not found then you insert a new post. If it is found then you update the post.
Let's say you have created your first piece of content and it's unique id is going to be jgohil_1. Your plugin might use something like the following to check for that unique id and then update or insert depending on if it exists.
<?php
// our meta key/value pair for our unique id
$meta_key = 'jgohil_unique_id';
$meta_value = 'jgohil_1';
// set our new content
$new_content = 'The new content here';
$new_title = 'The new title here';
// check the database to see if we have created the post already by querying for our unique id
global $wpdb;
$sql = $wpdb->prepare (
"SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
AND meta_value = %s",
$meta_key,
$meta_value
);
$post_id = $wpdb->get_var( $sql );
// if we got a post id then update else insert
if ( $post_id ) {
// set up the data for updating
$data = array(
'ID' => $post_id,
'post_content' => $new_content,
'post_title' => $new_title,
);
// update the post
$updated = wp_update_post( $data );
if ( is_wp_error( $updated ) ) {
// do some error handling here
}
} else {
$data = array(
'post_title' => $new_title,
'post_content' => $new_content
'post_type' => 'your_custom_post_type'
);
$post_id = wp_insert_post( $data );
// if the insert worked give the post the unique meta id
if ( ! is_wp_error( $post_id ) && $post_id !== 0 ) {
update_post_meta( $post_id, $meta_key, $meta_value );
} else {
// do some error handling here
}
}
If you need to handle multiple pieces of content at once you would want to use a loop and change the meta value each time.

Pulling data for WordPress Jetpack plugin contact form

I have a small form on my website, which I created using Jetpack plugin (it has built-in contact form creator).
I want to pull options for select input from my custom-post-type "artist" (using the titles of the posts). Is there a way to do it?
[contact-field label='Artist' type='select' required='1' options='i want my titles go here separated by commas plus "other" option'/]
The code for the field looks like this. I believe I need to do some php+jquery stuff in this page template, but I can't seem to get it.
With some creativity, yes, it's possible :)
We create another Shortcode to create a "virtual" JetPack shortcode.
I tested this using the default post post_type.
add_shortcode( 'my-jet-form', 'so_14003883_jet_form' );
function so_14003883_jet_form( $atts, $content )
{
// Query our post type, change accordingly
$posts = get_posts( array(
'post_type' => 'post',
'numberposts' => -1,
'post_status' => 'publish'
) );
// Build an array of post titles
$titles = array();
foreach( $posts as $post )
{
$titles[] = $post->post_title;
}
// Convert array into comma sepparated string
$posts_select = implode( ',', $titles );
// Make JetPack shortcode
$return = do_shortcode( '[contact-form][contact-field label="Name" type="name" required="1"/][contact-field label="Artist" type="select" options="' . $posts_select . '"/][/contact-form]' );
return $return;
}
Usage:
adjust the desired post type
adjust the do_shortcode part to suit your original shortcode
put the shortcode [my-jet-form] wherever you want
voilĂ 

Query all wordpress post titles

I am using Wordpress auto suggests using this snippet of code
and currently it is searching all tags, I want it to search only post titles. Any help is appreciated.
This is sql query calling all the tags which needs to be modified for all posts.
<?php global $wpdb;
$search_tags = $wpdb->get_results("SELECT name from wp_terms WHERE name LIKE '$search%'");
foreach ($search_tags as $mytag)
{
echo $mytag->name. " ";
}
?>
These days i had to do some request in a wordpress theme.
In your case ( getting title can be done easier than getting tags, as in your example link ) the stuff can be done easier (i guess).
Firstly you have to make a php page to get posts. As you maybe know you won't be able to use wp stuff in standalone php files, so your file ( let call it get_posts.php ) will look like
<?php
// Include the file above for being able to use php stuff
// In my case this file was in a folder inside my theme ( my_theme/custom_stuff/get_posts.php ).
// According to this file position you can modify below path to achieve wp-blog-header.php from wp root folder
include( '../../../../wp-blog-header.php' );
// Get all published posts.
$list_posts = get_posts( array( 'numberposts' => -1 ) );
// Get "q" parameter from plugin
$typing = strtolower( $_GET["q"] );
//Save all titles
$list_titles = array();
foreach( $list_posts as $post ) { $list_titles[] = $post->post_title; }
// To see more about this part check search.php from example
foreach( $list_titles as $title ) {
if( strpos( strtolower( $title ), $typing ) ){
echo $title;
}
}
?>
I added some comments trying to help you better.
Now stuff get easy, you only have to call your page through jQuery plugin like
$('#yourInput').autocomplete( "path_to/get_posts.php" );
You can directly use wordpress in-build feature to get all post titles
// The Query
query_posts( 'posts_per_page=-1' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
None of the answers here answer your real question:
How to query JUST post titles
The "raw SQL" way:
Few important things:
escape search for SQL! (also do that for the tags search!) use $GLOBALS['wpdb']->esc_like()
if you only need 1 column, you can use $GLOBALS['wpdb']->get_col()$GLOBALS['wpdb']->get_results() is if you want to fetch more columns in one row
use $GLOBALS['wpdb']->tableBaseName to make your code portable - takes care of the prefixe.g. $GLOBALS['wpdb']->posts
When querying posts you must also think about which post_type and post_status you want to query=> usually the post_status you want ispublish, but post_type may vary based on what you want
WordPress table "posts" contains ALL post types - post, page, custom, but also navigation, contact forms etc. could be there! => I strongly advice to use explicit post_type condition(s) in WHERE ! :)
...$GLOBALS is same as globalizing variabl -today performance difference is little
<?php
// to get ALL published post titles of ALL post types (posts, pages, custom post types, ...
$search_post_titles = $GLOBALS['wpdb']->get_col(
"SELECT post_title from {$GLOBALS['wpdb']->posts}
WHERE (
(post_status = 'publish')
AND
(post_title LIKE '{$GLOBALS['wpdb']->esc_like($search)}%')
)
ORDER BY post_title ASC
"); // I also added ordering by title (ascending)
// to only get post titles of Posts(Blog)
// you would add this to conditions inside the WHERE()
// AND (post_type = 'post')
// for Posts&Pages
// AND ((post_type = 'post') OR (post_type = 'page'))
// test output:
foreach ($search_post_titles as $my_title) {
echo $my_title . " ";
}
?>
The WP_Query way
This is more wordpress but has a little overhead, because although there is a fields param for new WP_Query()/get_posts(), it only has options:
'all' - all fields (also default), 'ids' - just ids, 'id=>parent' - ... if you pass anything else, you still get all, so you still need to add "all" BUT - WP however has filters for altering fields in SELECT.
I tried to make it the same as the raw SQL version, but it depends on how does WP does it's "search" - which I think is %search% for 1 word + some more logic if there are more words. You could leverage the $clauses filter used for fields to also add your custom where INSTEAD of adding the 's' into $args (remember to append to not-lose existing WHEREs $clauses['where' .= "AND ...;). Also post_type => 'any' does not produce always the same results as the raw query in cases like Navigation, Contact forms etc...
Also WP_Query sanitizes the input variables so actually don't escape $args!
<?php
$args = [
'fields' => 'all', // must give all here and filter SELECT(fields) clause later!
'posts_per_page' => -1, // -1 == get all
'post_status' => 'publish',
's' => $search,
// I also added ordering by title (ascending):
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'any', // all "normal" post types
// 'post_type' => 'post', // only "blog" Posts
// 'post_type' => ['post', 'page'], // only blog Posts & Pages
];
$onlyTitlesFilter = function($clauses, $query) {
// "fields" overrides the column list after "SELECT" in query
$clauses['fields'] = "{$GLOBALS['wpdb']->posts}.post_title";
return $clauses; // !FILTER! MUST RETURN!
};
$onlyTitlesFilterPriority = 999;
// add filter only for this query
add_filter('posts_clauses', $onlyTitlesFilter, $onlyTitlesFilterPriority, 2);
// Pro-tip: don't use variable names like $post, $posts - they conflict with WP globals!
$my_posts = (new WP_Query($args))->get_posts();
// !IMPORTANT!
// !remove the filter right after so we don't affect other queries!
remove_filter('posts_clauses', $onlyTitlesFilter, $onlyTitlesFilterPriority);
// test output:
// note that I used "my"_post NOT just $post (that's a global!)
foreach($my_posts as $my_post) {
echo $my_post->post_title . " ";
}
?>
Don't be confused - you will still get the array of WP_Posts - WP will throw some default properties&values into it, but in reality it will only query and fill-in with real values the fields you specify in the filter.
PS: I've tested these - they are working codes (at least on WP 5.4 and PHP7 :) )

Categories