set post author depending on condition via frontend post submit - php

I use wordpress cms and I allow users to post from frontend. There are three kinds of users. Myself as admin, a junior ( given a role of an author) and anyone from public as non-logged-in user. Currently if any non-logged-in user posts from frontend, I am assigned as an author with this code. Currently my wp_insert_post array looks something like this :
'post_title' => $final_title,
'post_content' => $about_stuff,
'post_status' => 'draft',
'post_author' => '20',
'post_type' => 'post',
etc....
wherein my author id is '20'. All this works fine. Now, what I would like to achieve is when my junior logs-in and creates post in the frontend, I would like him to be the post author, and not me (as currently set). I understand that the post_author will need to be a variable example $content_creator. Given below is the code I have written till now, but I am confused how to put it together in order to achieve what I need. To be specific I am confused how do I make the variable $content_creator work with rest of the code.
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( 19 == $current_user->ID ) {
'post_author' => '19';
} else {
'post_author' => '20',
}
}
The code is pretty self-explanatory and basic which says check if the user if logged in, if yes check the user-id. If user id equals 19 set 'post_author' => '19' (user id of my junior) otherwise set the author as admin. Two things I wanted to ask, do I also need global $post before my code and should I be using another filter wp_update_post instead. Please help.
Final scenario must be, when the admin or anyone else creates a post the post-author must be set as admin (me) but when my junior creates a post he must be set as the post-author. This would have been unnecessary if we were creating posts in the backend but due to some reason we prefer to create posts in frontend.

Turns out it was pretty simple with wp_update_post. Although, now it works fine. Let me know if you find something funny. Here is the code I am using after wp_insert_post and did not make any change in the array.
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( 19 == $current_user->ID ) {
$update_creator = array(
'ID' => $pid, //created post ID
'post_author' => '19'
);
wp_update_post( $update_creator );
}
}

Related

Gutenberg having conflict with my plugin short code and seems to be displaying on editor page

I have made my first plugin and it all works on the front end but I am having some weird backend conflict with the gutenberg. I have done two things, made my plugin automatically add the pages / overwrite them when they've checked off, and made the plugin shortcode.
Here is the problem on my gutenberg:
As you can see there are random red diamonds on the page. When I use the inspector I can see it has code from my plugin I created...
Here is how I created my plugin
First inside my functions page I registered a new short code.
add_shortcode('parts_listing_portal', 'portal_parts_list');
function portal_parts_list(){
include "shortcodes/parts_listing_shortcode.php";
return ;
}
Then I made content for inside the shortcode using JS, PHP, and HTML.(this is a big file with includes and API calls)
After the page looked the way I would like, I created a new function to work on my option submit. This function creates the page with the shortcode inside.
//Add parts
if ( get_option( 'parts' ) === "checked") {
$parts_list = array(
'post_title' => 'Parts',
'post_type' => 'page',
'post_name' => 'Parts',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '[parts_listing_portal]',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0
);
// PARTS LIST
if(get_page_by_title('Parts') == null) {
wp_insert_post($parts_list);
}else{
$post_exists = get_page_by_title("Parts" );
$update_post = array('ID' => $post_exists->ID);
$post_update = wp_parse_args($parts_list, $update_post);
wp_update_post($post_update);
}
When my function adds to the page & you go to the gutenberg editor it shows the full page for a split second then goes away. I feel I missed a statement somewhere to only show on "page" but I have no idea where to look. I've spent a lot of time researching it but couldn't find it.
Thank you
Whenever adding a shortcode make sure to check iif in admin area. If in admin area return nothing
function portal_parts_list(){
if ( is_admin()){
return;
}else {
include "shortcodes/parts_listing_shortcode.php";
return ;
}
}
add_shortcode('part_description_portal', 'portal_parts_list');

Function to auto-delete wordpress media after period of time

I'm writing what I thought was a simple series of functions to assign a "deletion date" to media, then auto-delete those expired media when the site is visited.
The post_meta for deletion date is set for the images via an online form when the images are uploaded (using formidable pro forms and the "after_create_entry" hook. I can confirm that the meta field is created successfully, and the deletion date is assigned properly (called 'mtp_deletiondate'. To test, I did a wp_query on a custom page and each image has a properly set deletion date.
Next, I wanted to run a function that checked for expired images (set to be 21 days after date of upload), and if they are expired, to delete them. I want to trigger this function whenever I access the admin, because I figure I get in there at least once a month to run updates, and it's a good time to clean out the old images. For this situation, I decided not to do a cron job since I can reliably visit the site once a month anyway.
The issue is the function doesn't seem to be triggering, and I don't know if it's because the function is broken or because I'm using the wrong action, or both. Some research tells me to use the admin_init, but I've also tried "wp", and "wp-footer". It might be that I'm just misunderstanding the process. My function (in my theme's functions.php):
function drick_delete_expired_uploads() {
// WP_Query arguments
$args = array (
'post_status' => 'any',
'post_type' => array( 'Attachment' ),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'mtp_deletiondate',
),
),
);
// The Query
$mediaquery = new WP_Query( $args );
// The Loop
if ( $mediaquery->have_posts() ) {
while ( $mediaquery->have_posts() ) {
$mediaquery->the_post();
date_default_timezone_set('America/Denver');
$CurrentDate = date('Y-m-d h:i');
$CurrentDateStr = strtotime($CurrentDate);
$DeletionDate = get_post_meta( $post->ID, 'mtp_deletiondate', true );
$DeletionDateStr = strtotime($DeletionDate);
if ( isset($DeletionDateStr) ) {
if ( $DeletionDateStr < $CurrentDateStr ) {
wp_delete_attachment( $post->ID, true );
}
}
}
} else {
// no posts found
} // IF HAVE POSTS
// Restore original Post Data
wp_reset_postdata();
}
add_action('admin_init', 'drick_delete_expired_uploads');
If I save my functions.php, then reload the Wordpress dashboard, then check my media, the expired images are still there. HOWEVER, if I add this function to an actual page then visit the page, it does work. So I believe the function is doing what it's supposed to, it's just not getting triggered properly? I also added a quick wp_mail() to the function in my functions.php, and when I visited the admin it did trigger the email, so I guess the function is firing.
I would appreciate any insight, thank you!
So I think I figured it out, but I don't know that I have an explanation as to why it works. Essentially it looks like wp_delete_attachment wasn't working, but wp_delete_post DOES work. I've tested and confirmed with three additional images that the function auto delete is triggered when accessing the admin dashboard. I also changed the way the query arguments are structured by only querying the expired images, rather than querying ALL images that have a mtp_deletiondate meta then running a conditional statement within the query. Don't know if that's related or not. The final working function:
function drick_delete_expired_uploads() {
// WP_Query arguments
date_default_timezone_set('America/Denver');
$CurrentDate = date('Y-m-d h:i');
$args = array (
'post_status' => 'any',
'post_type' => array( 'Attachment' ),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'mtp_deletiondate',
'value' => $CurrentDate,
'compare' => '<',
'type' => 'DATE',
),
),
);
// The Query
$mediaquery = new WP_Query( $args );
// The Loop
if ( $mediaquery->have_posts() ) {
while ( $mediaquery->have_posts() ) {
$mediaquery->the_post();
wp_delete_post(get_the_ID());
}
} else {
// no posts found
} // IF HAVE POSTS
// Restore original Post Data
wp_reset_postdata();
}
add_action('admin_init', 'drick_delete_expired_uploads');
Still look forward to any feedback from someone in the know that can tell me why the previous version wasn't working (looking to learn here). Thanks!

Check for duplicate Wordpress Post with Custom Meta Data on Publish

I have a site that uses a custom meta-box with the following fields using Meta-Box plugin. Code is as follows
`
$meta_boxes[] = array(
'title' => 'MLS ID',
'pages' => array('property'),
'fields' => array(
array(
'name' => 'MLS ID',
'id' => "IntegratorPropertyID",
'desc' => 'MLS: e.g. 240091025-217',
'type' => 'text',
),
array(
'name' => 'Test MLS',
'id' => "mlsTest",
'desc' => 'Test MLS for Duplicate',
'type' => 'button',
),
),
'validation' => array(
'rules' => array(
"IntegratorPropertyID" => array(
'required' => true
),
),
'messages' => array(
"IntegratorPropertyID" => array(
'required' => 'MLS is required',
),
)
)
);
Now what im looking for is to add an 'add_action( 'save_post', 'checkMLS' );' function that checks all previous CPT property for MLS number to make sure it hasn't been input before. The code I used was:
function checkMLS( $post_id ) {
$slug = 'property';
if ( $slug != $_POST['post_type'] ) {
return;
}
$mls2 = rwmb_meta('IntegratorPropertyID', 'type=text', $post_id);
$args = array( 'post_type' => 'property' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$this1 = get_the_ID();
$mls1 = rwmb_meta('IntegratorPropertyID', 'type=text', $this1);
if ( $mls2 == $mls1 ) {
$my_post = array(
'ID' => $post_id,
'IntegratorPropertyID' => 'DUPLICATE!'
);
wp_update_post($my_post);
return;
}
endwhile;
}
add_action( 'save_post', 'checkMLS' );
That code is found in the functions.php and when I try to post the screen goes white. Debugging mode does not offer any help either. :/
I'm sure I'm making some programming Major mistake somewhere. Can someone point it out? or maybe point me in the right direction? or suggest something completely different?
Thanks
Keith
OK. Firstly, your white page with no indication of why, is probably either an 'out of memory' error, or a php 'max execution time' error. This stims from one major flaw in the way the checkMLS() function works. The flaw is that you are literally cycling through ALL 'property' posts in your database. Depending on the size of your dataset, this can be a LOT, especially considering you are dealing with MLS lists.
MY RECOMMENDATION:
Figure out how the rwmb_meta() function is grabbing it's information. It is probably just a wrapper function for the get_post_meta() function, but maybe not. Assuming that it is, I propose doing the following, which I will explain the details of after as well as in the comments:
// the save_post action runs after a post has been saved/created, and has two parameters
// param 1: the id of the post
// param 2: the post object
function checkMLS($post_id, $post) {
// use the post object post_type to determine if this is a property or not.
// it will be a lot more reliable
if ($post->post_type != 'property') return;
// meta_key should be equal to the 'meta_key' field in the wp_postmeta table, for the
// id you are trying to check against. your example used IntegratorPropertyID.
// again you may want to check rwmb_meta() function to figure out if there is a
// 'prefix' or 'suffix' added to this. despite that, it is almost certainly going to
// be looking in the wp_postmeta table, so this should work nicely
$meta_key = 'IntegratorPropertyID';
// look up the current mls id for this post, which you just saved/created
$mls_id = get_post_meta($post_id, $meta_key, true);
// lookup in the actual database table for any matching row, that has the same MLS id
// that is not this post.
global $wpdb;
$q = $wpdb->prepare('select post_id from '.$wpdb->postmeta.' where meta_key = %s and meta_value = %s and post_id != %d limit 1', $meta_key, $mls_id, $post_id);
$exists = $wpdb->get_var($q);
// if it already exists, mark the value as a duplicate
if ($exists) update_post_meta($post_id, $meta_key, 'DUPLICATE!');
}
// add your check function late in the actions, at priority 10000
add_action('save_post', 'checkMLS', 10000, 2);
From the top, we create a callback with two params, because the save_post action sends two, $post_id and $post. Since save_post runs after the post has been saved, you already have an object ($post) which has all the post info in it. We can then use that $post object to determine the type of the post, which is more reliable than looking at a $_REQUEST value, mainly because $post is pulled directly from the database and passed to you.
Now, as stated before I assume that rwmb_meta() is just a kinda wrapper function for get_post_meta(). It probably adds a prefix or suffix to the $meta_key, but a little research into the rwmb_meta() function should tell you how the $meta_key is changed when passing it to the get_post_meta() function, and you can modify $meta_key from there. With the correct $meta_key, we can now get the MLS id of the property you just saved.
With that MLS id, we need to do a direct lookup in the database, to determine if there is another property with that id already. While the way in your demo function does work on small sets of data, there is no way it would work on any appreciable amount of properties. Thus the direct approach is needed. Simply we craft some special SQL to look in the wp_postmeta table for any post_id that has an MLS id that is equal to the one entered for this property, that is not this property. If we find one match that is not this property, then it is a dupe. If it is a dupe, we need to mark it as a dupe.
Notice that this solution does not do any looping at all. There is no potential for it to loop over 10000 records to find a dup id. This is streamlined. It looks up the id directly in the db, to see if there are dups.
Hopefully this is helpful to you, and hopefully others find it helpful as well. My company does WordPress work, almost exclusively. Through our years of working with WordPress we have encountered problems from the super simple to the overly complex. This same problem, in different settings, has manifested with many of our clients. This solution is simple and to the point, though highly custom. It will however, work.

Display, sort and filter posts from a relationship field (Podscms)

I've got some custom post types set up in Wordpress using Pods and linking them using relationship fields. Now I'd like to display (and link to) the related custom posts 'postB' from a single post 'postA'. I also just want to display those posts which got a date in the future, which is also stored in a custom field in 'postB'.
This is what I've currently got so far, put into a theme template file (single-posta.php):
<?php
$id = get_the_ID();
$params = array(
'where' => 'postB.ID IN ('.$id.')',
'limit' => -1, // Return all
//'oderby' => 'postB.customDate, (order is not working, so I commented it out)
//'order' => 'ASC'
);
$postsB = pods( 'postB', $params );
if ( 0 < $postsB->total() ) {
while ( $postsB->fetch() ) {
?>
<p>
<?php echo $postsB->display( 'title' ); ?><br>
<?php echo $postsB->display( 'customDate' ); ?><br>
</p>
<?php
}
}
?>
So how can I
order the results?
link to these posts?
limit them to dates in the future?
Btw. is this the right way to get those posts anyway?
You could use WP_Query too, but since you're using the Pods find() syntax, I'll give you the correct code for what you're after using that:
$params = array(
'where' => 'postB.ID IN ('.$id.')',
'limit' => -1, // Return all
'orderby' => 'customDate.meta_value ASC'
);
$postsB = pods( 'postB', $params );
Pods doesn't let you create fields with capital letters though, so it's likely you created that one outside of Pods, correct? Just double checking, if it was created with Pods it would be named 'customdate'

How to inject a virtual post (i.e. without adding it to the database)

I use a WordPress blog and I want to show a post without adding anything to database.
What I want to say is:
I generate a post when page loads,and prepend it in homepage.
I've searched and found wp_insert_post() function but it also add to database.
How can i do this with php?
For example:
There is a post array which is generated by a query.How can I insert my post to this array before page loaded?
I want clear my idea.Here's step by step what i want.
*1)*Im generating an array like that
$arr['title] = "my title",
$arr['content'] = "my content",
*2)*WP sends a query to database and have the posts am i right? And there is an array,to show on the theme and main page?
At this point i want to add my external array(generated in step1 ) to this array(generated by WP via a query)
3) By this way i will be able to add a post without adding it to my database.
You can simply add your virtual post in one of your theme templates as raw HTML.
Alternatively, if you're feeling adventurous, you could modify the main query results and include your post inside:
add_action('loop_start', function($query){
// create the post and fill up the fields
$post = new WP_Post((object)array(
'ID' => -1,
'post_title' => 'Bla blah',
'post_content' => 'Your content',
));
// add it to the internal cache, so WP doesn't fire a database query for it
// -1 is the ID of your post
if(!wp_cache_get(-1, 'posts'))
wp_cache_set(-1, $post, 'posts');
// prepend it to the query
array_unshift($query->posts, $post);
});
The currently accepted answer causes the new post to delete the last post in the loop, because it doesn't update the post count. Here's my modified version that also includes:
Support for empty categories.
Only one place to declare the new post's ID.
Adding is_main_query() as the person who originally answered mentioned in a comment.
A setting to decide if the new post should be appended or prepended.
Hiding the post's date because otherwise you get something like 00000000. I could have used a dynamic date but it may be bad SEO to keep updating the date without updating the content.
Hiding the post's comment link because it just leads to the homepage.
A setting to control the post type. You might prefer "page" because "post" displays a general category, which I found no way to bypass. "Page" also looks more distinguished among other posts, assuming that's a good thing.
Here's the modified code:
function virtual_post($query) {
$post_type = 'page'; // default is post
if (get_class($query)=='WP')
$query = $GLOBALS['wp_query'];
if ($query->is_main_query()) {
$append = true; // or prepend
// create the post and fill up the fields
$post = new WP_Post((object)array(
'ID' => -1,
'post_title' => 'Dummy post',
'post_content' => 'This is a fake virtual post.',
'post_date' => '',
'comment_status' => 'closed'
));
if ($post_type <> 'post')
$post->post_type = $post_type;
// add it to the internal cache, so WP doesn't fire a database query for it
if(!wp_cache_get($post->ID, 'posts')) {
wp_cache_set($post->ID, $post, 'posts');
if ($query->post_count==0 || $append)
$query->posts[] = $post;
else
array_unshift($query->posts, $post);
$query->post_count++;
}
}
}
$virtual_post_settings = array('enable' => true, 'include_empty_categories' => true);
if ($virtual_post_settings['enable']) {
if ($virtual_post_settings['include_empty_categories'])
add_action('wp', 'virtual_post');
else
add_action('loop_start', 'virtual_post');
}

Categories