Automatically close comments on articles older than X days also hides comments - wordpress

I am using WordPress 4.0.
My goal is to disable new comments on posts that are older than 14 days, but still have existing comments, that were made in the allowed time frame, be visible.
In Settings->Discussion, there is an option to "Automatically close comments on articles older than x days". I have selected this option, and set x to be 14 days.
I thought this would simply close comments on posts older than 14 days , but it is also hiding the comments for posts older than 14 days.
I have checked the theme's comments.php file, and I don't see anything that would indicate that it is the theme that is hiding the comments.
I am relatively new to Stack Overflow. I have googled this question all day, and searched Stack Overflow, and cannot find an answer. So I guess my question is, if the code that is causing this behavior is not in comments.php, where would it be?
Thank you.

If you want to disable comments on your posts after specific number of days, you can use this code for the purpose. Put this code in your theme's functions.php file.
<?php function autoclose_comments() {
global $wpdb, $tableposts;
if (!isset($tableposts))
$tableposts = $wpdb->posts;
$age = '21 DAY';
$date = $wpdb->get_var("SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL $age), INTERVAL 1 DAY)");
$wpdb->query("UPDATE $tableposts SET comment_status = 'closed' WHERE comment_status = 'open' AND post_status = 'publish' AND post_date < '$date'");
}
You can set $age as per your requirement.

Related

Cron Wordpress Update four post every hour

I have function that update post content automatically based from custom field like this:
function update_content(){
$mycustomfield = get_post_meta( get_the_ID(), 'customfield', true);
$post = array();
$post['ID'] = get_the_ID();
$post['post_content' ] = $mycustomfield ;
$post['post_title' ] = $mycustomfield ;
// Update the post into the database
wp_update_post( $post );
We update only custom field to make content. For now, we launch this function manually on save_post hook, but the articles are so many and we need now a cron to automate this function: process 4 posts every hour until all posts are completed, then start over.
How to make this, thank you
WordPress actually has a built-in psuedo cron system called WP Cron. It doesn't function exactly like a proper server cron, but can perform a similar function in many cases. You can find documentation on it here:
https://developer.wordpress.org/plugins/cron/#:~:text=WP%2DCron%20is%20how%20WordPress,post%2C%20utilize%20WP%2DCron.&text=WP%2DCron%20works%20by%20checking,what%20needs%20to%20be%20run.
However thinking about your use case and looking at your function above, I'm wondering what the purpose of your cron is? It looks from that function like all you're doing is taking some content already in your database and putting it somewhere else. Why? Why not simply display your custom field in the correct spot? Or better yet, use the fields as intended?
Even if that is necessary - maybe I don't understand fully from the example above - I think your initial inclination to run this on save_post is much more correct. Unless there's some information external to your site that's changing, the only time these values will change is when you save the post. So what is the purpose of running it on a schedule?

show wp comment as rejected to all users

I would like to be able to "reject" a comment in wp's backend without altering or deleting the comments contents.
On the front end of the website I would like have the comment authors name listed as normal but instead of the comments contents have a simple statement that reads "This comment was rejected for violations of our TOS"
How would I go about this? There doesnt seem to be a plugin that I have found to handle this function.
You can use below code snippet. This snippet will filters the text of a comment to be displayed.
function filter_text( $comment_text, $comment = null ) {
// Do something to the comment, possibly switching based on the presence of $comment
$comment_text = 'This comment was rejected for violations of our TOS';
return $comment_text;
}
add_filter( 'comment_text', 'filter_text', 10, 2 );

How can I exploit the wordpress template hierarchy to render a different post template depending on category name/slug?

Apologies if this appears simple to some, but I have scaled high and low and I'm not finding a solution here to my problem, which is:
I have a website set up with Wordpress in which posts can fall under one of three categories: reviews, views, news - the slugs associated with each of these category names are the same.
Currently, calling up the web page of any individual post classified under any of these categories will see it rendered by the file single.php.
However, I want to make a slight adaption to the rendering of the post when it falls within the 'reviews' category. I have copied and renamed the original single.php file to single-post-reviews.php (no custom posts here, I will just confirm and I would like, if possible, to avoid child-theming here - not good practice, I know), but I am not seeing the new rendering from my new file.
I've also tried renaming to single-reviews.php which hasn't worked either - so could someone tell me what exactly I'm missing here?
Thanks,
WordPress Template Hierarchy for Single Posts doesn't factor in the current post category (likely due to the fact you can have multiple categories). Due to this, you have 2 viable options to your problem.
1) You can modify single.php to check for the post category, and if it's categorized under reviews, do something. This makes sense if you're just adding a small amount of markup in one or two places, or even hiding a few lines conditionally.
2) You can override the page template that's loaded based on the post's category using the single_template filter. Because I don't know exactly what you're doing, I'm going to elaborate more on this method. Take the following function:
add_filter( 'single_template', 'so51913799_review_template' );
function so51913799_review_template( $single_template ){
global $post;
if( has_category( 'reviews' ) ){
$single_template = get_stylesheet_directory() . '/single-post-reviews.php';
}
return $single_template;
}
If you put it in your functions.php file, it will make use of the has_category() function (I prefer this to in_category() since in_category just returns has_category anyways) and if it matches, it will update the $single_template variable to single-post-reviews.php. This assumes that the file is inside your /wp-content/themes/ACTIVE-THEME/ directory.

Buddy Press sort by last name?

I’ve been trying to get a Members Directory to sort by last name, but have had no luck. Any help would be much appreciated.
The directory is here: http://109.199.101.20/~newfeldenkrais/practitioner-search/results/
The search is powered by the plugin BP Profile Search, but is displaying on the default members page.
Versions:
WP – Version 4.7.5
BP – Version 2.8.2
BP Profile Search – Version 4.7.6
I’ve tried various solutions that I was able to find on Google with no luck, the most recent solution I could find was here: https://buddypress.org/support/topic/custom-searchfilter-based-on-custom-field/
It looks like that solution is looking for the space between the First Name and Last Name to target the last name, but BuddyPress seems to be grabbing the “nicename”, which has no space. So, it isn’t able to target the space for me?
Use this hook in functions.php,
public function alphabetize_by_last_name( $bp_user_query ) {
if ( 'alphabetical' == $bp_user_query->query_vars['type'] )
$bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.display_name, ' ', -1)";
}
add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' );
Ref : https://gist.github.com/mgmartel/4463855
https://buddypress.org/support/topic/sort-user-list-by-last-name/

RSS feed doesn't validate

I get this error on my default WordPress feed: pubDate must be an RFC-822 date-time: Mon, 30 Nov -0001 00:00:00 +0000
Anything inherently evil in pubDate? And if so how to solve the problem?
If you look at the two occurrences of the error from Feed Validator, it's only happening because of the oldest two posts in the feed. Every post after that has the correct pubdate specification. WordPress itself follows the specification, but those two posts are missing the year.
This answer might be of use, as it's what's occurring in your posts
There are 4 dates stored for each WP post: Post_date, post_date_gmt,
post_modified and post_modified_gmt. I recommend you to insert the
same date for both post_date and post_modified and see if it works.
That worked, thanks! It was just the post_date_gmt field that needed
to be set.
If it's just those two, you can manually edit them in the wp_posts table in your database if you feel comfortable doing so, or there are various plugins available that can help with doing such a thing.
PSA: Always have a backup of your database before fiddling with it.
Upgrage your plugins with newer version here https://wordpress.org/plugins/wordpress-seo/
because there is a bug
https://github.com/Yoast/wordpress-seo/issues/525
but if it has not been solve your problems, then better you remove this two post
your.url/cliff-diving-boracay-philippines
your.url/bora-bora-activities-snorkeling-hibiscus
or make new version of those.
Try this .. might work
Go into your wp-includes directory and edit the feed-rss2.php file.
Replace this (line no : 89):
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
With this:
<pubDate><?php echo mysql2date('r', get_the_time('Y-m-d H:i:s')); ?></pubDate>
And also try to update the Post for which the date is wrong :
post ID = 17624 and 17637 or see the published date of these posts in edit screen

Resources