Multiple uri to same post in Wordpress - php

I wanted to make two uri to point same post in Wordpress but without redirect,
mysite.co.uk/awesome-post
mysite.co.uk/?p=12
I want those two uri to be pointed to same post but when you reach the page uri shouldn't change.

The only solution that i know is duplicate your post and get second link from it and hide the duplicate from loop and if you wish back end.
for that you have to use save_post action and plugin/theme activation hook for duplicate posts that have already published (this can be init hook but with caution you need to do this only one time).
first you have to loop through all posts and make duplicate
add_action('switch_theme', 'your_prefix_setup_options');
function your_prefix_setup_options () {
// WP_Query arguments
$args = array (
'post_type' => array( 'post' )
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// duplicate post like this
your_prefix_post_duplicate( get_the_ID(), get_the_title(), get_the_content(), get_post_thumbnail_id() );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
function your_prefix_post_duplicate($post_id, $title, $content, $attachment){
$post_id = $post_id;
$post_name = $title;
$content = $content;
$attachment = $attachment;
$slug = str_replace( " ", "-", $post_name );
$slug = strtolower($slug);
$post_data = array(
'post_content' => $content,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 0,
'post_name' => $slug,
'post_title' => $post_name,
'post_status' => 'published',
'post_type' => 'post'
);
$prefix = 'your_prefix__';
//create your duplicate post
$duplicate_post_id = wp_insert_post( $post_data );
set_post_thumbnail( $duplicate_post_id, $attachment );
add_post_meta( $duplicate_post_id, $prefix . 'duplicate', TRUE );
//get duplicate link
$perma_link = get_permalink ( $duplicate_post_id );
//set this link to your post as meta
add_post_meta( $post_id, $prefix . 'duplicate_url', $perma_link );
}
// now you can get second uri for fb like/share
$second_link = get_post_meta(get_post_ID(),$prefix . 'duplicate_url', true);
//and you can use the meta and ignore duplicate post from showing on loop
// or in advance you can use pre get post to hidethese duplicates from front end & back end permanently
get_post_meta(get_post_ID(),$prefix . 'duplicate', true);

Related

Wordpress modifies content when importing post

I use the PHP script below to import new posts into Wordpress.
require_once("wp-load.php");
$url = 'https://somejsonfeed.com'; // path to the JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$flights = json_decode($data, true); // decode the JSON feed
foreach ($flights['response'] as $item) {
$title = $item['flight']['iata_number'];
if ( ! is_admin() ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
if ( get_page_by_title( $title, OBJECT, 'custom-post-name' ) == null ) {
// Insert post
$new_post = array(
'post_title' => $title,
'post_content' => '[jsoncontentimporterpro url=yes url=https://someapiwithakey123abc&iata_code=<cf_wpcf-arrival-airport\> value&type=something]',
'post_status' => 'draft',
'post_author' => 1,
'post_type' => 'custom-post-name'
);
// Insert post
$post_id = wp_insert_post($new_post);
// Insert post meta if available
add_post_meta( $post_id, 'meta_key', 'meta_value' );
}
}else{
// Update meta value
update_post_meta($get_page->ID, 'my_key', 'meta_value');
}
But after importing the content it removes <cf_wpcf-arrival-airport\> and shows the content below in the Wordpress WYSIWYG editor.
[jsoncontentimporterpro url=yes url=https://someapiwithakey123abc&iata_code= value&type=something]
Any idea how I can fix this?
No perfect solution, but a work around.
Used the https://nl.wordpress.org/plugins/insert-html-snippet/ plugin and imported the post-content value into a snippet. Then I used the snippet in the file above.

Check if title exists in wordpress database before import

I am using a json feed to import new posts. This works as expected, but I am not able to prevent importing duplicated posts. When I run the script twice it also imports the posts twice.
How can I check the wordpress database to prevent importing the same posts.
require_once("wp-load.php");
$url = 'https://somejsonfeed.com'; // path to the JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$flights = json_decode($data, true); // decode the JSON feed
foreach ($flights['response'] as $item) {
$title = $item['flight']['iata_number'];
// Check if already exists
if ( get_page_by_title( $title ) == null ) {
// Insert post
$new_post = array(
'post_title' => $title,
'post_content' => $title,
'post_status' => 'draft',
'post_author' => 1,
'post_type' => 'post'
);
// Insert post
$post_id = wp_insert_post($new_post);
// Insert post meta if available
add_post_meta( $post_id, 'meta_key', 'meta_value' );
// Uncomment to check if meta key is added
// $get_meta_value = get_post_meta( $post_id, 'meta_key', true );
// echo "<pre>";
// print_r($get_meta_value);
}
}else{
// Update meta value
update_post_meta($get_page->ID, 'my_key', 'meta_value');
// Uncomment to check if meta key is added
// $get_meta_value = get_post_meta( $get_page->ID, 'meta_key', true );
// echo "<pre>";
// print_r($get_meta_value);
}
What works well for me is post_exists($title). Returns the post ID, so in your code, I would use
$current_post_id = post_exists($title);
if( 0 === $current_post_id) {
`enter code here`add_post_meta( $post_id, 'meta_key', 'meta_value' );
}
One thing I notice is that you have:
'meta_key', 'meta_value', so of course, I copied that part, but you would need to replace whatever meta_key and meta_value.
I hope this helps.
Bruce
Solved by using the code below
require wp-load.php to use built-in WordPress functions
require_once("wp-load.php");
$url = 'https://somejsonfeed.com'; // path to the JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$flights = json_decode($data, true); // decode the JSON feed
foreach ($flights['response'] as $item) {
$title = $item['flight']['iata_number'];
if ( ! is_admin() ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
// change custom-post-name in post type
if ( get_page_by_title( $title, OBJECT, 'custom-post-name' ) == null ) {
// Insert post
$new_post = array(
'post_title' => $title,
'post_content' => $title,
'post_status' => 'draft',
'post_author' => 1,
'post_type' => 'post'
);
// Insert post
$post_id = wp_insert_post($new_post);
// Insert post meta if available
add_post_meta( $post_id, 'meta_key', 'meta_value' );
// Uncomment to check if meta key is added
// $get_meta_value = get_post_meta( $post_id, 'meta_key', true );
// echo "<pre>";
// print_r($get_meta_value);
}
}else{
// Update meta value
update_post_meta($get_page->ID, 'my_key', 'meta_value');
// Uncomment to check if meta key is added
// $get_meta_value = get_post_meta( $get_page->ID, 'meta_key', true );
// echo "<pre>";
// print_r($get_meta_value);
}

How to save data in a custom post type in wordpress before sending the email?

I have a problem in Wordpress. I want to make a hook on Contact Form 7, so that when the user clicks send, it first saves this information inside a custom post type.
Reading the documentation, I found this
// run the action
do_action( 'wpcf7_before_send_mail', $contact_form );
// define the wpcf7_before_send_mail callback
function action_wpcf7_before_send_mail( $contact_form ) {
//code
};
// add the action
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );
But I do not know how to continue. Has anyone done this and can you help me please?
function action_wpcf7_before_send_mail( $contact_form ) {
$post_content = ''; // empty contebt
foreach ($_REQUEST as $key => $value) {
$post_content .= $key.': '.$value.'
'; //add each form field to content
}
$title = $_REQUEST['some field'].' '.$_REQUEST['some field2']; // generate dynamic title
$t = time();
$thash = md5($t);
$my_query = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $post_content,
'post_type' => 'your-post-type',
'post_name' => $thash,
'post_status' => 'publish',
'post_author' => 1
);
$data = wp_insert_post( $my_query );
return $contact_form;
};
// add the action
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );

Get images from gallery in taxonomy

There seems to be several answers to similar questions but I have not yet found one working for me.
I have a custom post-type called entertainement. entertainement has a taxonomy called ent_categories.
One of the ent_categories is called Event
Each Event has a gallery and I am trying to make a query that will return the latest 10 images that has been added to any of the CPT entertainment with the category Event.
Im hoping to receive a list of urls in an array.
From what I read here something like this should do the trick:
$arg = array(
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_type' => 'attachment',
);
$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => array( 'Event' ),
),
);
$the_query = new WP_Query( $arg );
var_dump($the_query);
The var_dump($the_query); displays a lot of things but no images?
Any tips on this one?
Thank you
EDIT:
I just saw that I can do this:
function pw_show_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$image_list = '<ul>';
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
$image_list .= '<li>' . $image . '</li>';
}
}
$image_list .= '</ul>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'pw_show_gallery_image_urls' );
This result in that all the gallery images urls get displayed below the images in the gallery.
Maybe this function could be called from a page instead than from functions.php?
You were on the right track, but you're querying for posts of type attachment with a term of the ent_categories taxonomy which only applies to entertainement posts, so there won't be any of them as you'll see if you:
var_dump($the_query->posts);
If you dump all $the_query you'll see lots of things because it's a WP_Query object.
You need to query your entertainement posts: (Be careful because you have a typo in your slug!)
$arg = array(
'posts_per_page' => -1,
'post_type' => 'entertainement',
);
$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => 'Event',
),
);
$the_query = new WP_Query( $arg );
Then you can iterate the posts and get the gallery items like this:
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
if (get_post_gallery()) :
echo get_post_gallery();
print_r(get_post_gallery_images());
endif;
}
/* Restore original Post Data */
wp_reset_postdata();
}
get_post_gallery_images() will get you an array of the URL's of gallery images
get_post_gallery() will get you actual HTML to print the gallery.

Wordpress - Frontend form Post title automatic generation using authors name

I'm using Advanced custom fields and custom post types ui, I need to generate a post name with the author's name in it, however this just prints "Solicitud", it seem like my variable $autor gets no value, is there any way I can fix this?
function my_pre_save_post( $post_id ) {
$post2 = get_post($post_id);
$autor=$post2->author;
// Create a new post
$post = array(
'post_status' => 'publish',
'post_title' => 'Solicitud' . $autor,
'post_type' => 'solicit',
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
And, here is the code I use to create the form, I'm using acf_form:
$current_inv = $_GET['invid'];
/**
* Template Name: Solicit
*/
acf_form_head();
get_header();
?>
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php acf_form(array(
'post_id' => 'new',
'field_groups' => array( 243 ),
'submit_value' => 'Crear el ticket'
)); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Try using 'post_author' instead of 'author'. And also make sure that WP_Debug is set to TRUE while you're developing.
Here's a long version
function my_pre_save_post( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'publish',
'post_title' => 'Solicitud',
'post_type' => 'solicit',
);
// insert the post
$post_id = wp_insert_post( $post );
// Once we save, retrieve the original post so we can take the post_author
$post2 = get_post($post_id);
// Use post_author
$autor = $post2->post_author;
// Update the post with the new title
wp_update_post(array('ID' => $post_id, $post2->post_title . $autor));
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
Slightly shorter version which uses the current logged in user's username.
function my_pre_save_post( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
$current_user = wp_get_current_user();
$author = $current_user->user_login; // OR [user_firstname, user_lastname, display_name]
// Create a new post
$post = array(
'post_status' => 'publish',
'post_title' => 'Solicitud' . $author,
'post_type' => 'solicit',
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
References:
http://codex.wordpress.org/Class_Reference/WP_Post
http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/
http://codex.wordpress.org/Function_Reference/wp_get_current_user

Categories