Redirect based on user post count

add_action( ‘template_redirect’, ‘redirect_to_specific_page’ ); function redirect_to_specific_page() { global $post; $current_user = $post->post_author; if(!empty($current_user)){ $user_post_count = (int) count_user_posts( $current_user ); if ( is_page(‘butiktest’) && $user_post_count == 1 ) { wp_redirect( ‘enhytte’, 301 ); exit; } } } authorized user: $current_user = wp_get_current_user();

How to add an admin function only to posts, not pages?

You can use post type conditional tag: if ( ‘post’ == get_post_type() ) Complete: function excerpt_count_js(){ if ( ‘post’ == get_post_type() ) { echo ‘<script>jQuery(document).ready(function(){ jQuery(“#postexcerpt .handlediv”).after(“<div style=\”position:absolute;top:0px;right:5px;color:#666;\”><small>Character limit = 150. Current characters: </small><input type=\”text\” value=\”0\” maxlength=\”3\” size=\”3\” id=\”excerpt_counter\” readonly=\”\” style=\”background:#fff;\”>&nbsp;</div>”); jQuery(“#excerpt_counter”).val(jQuery(“#excerpt”).val().length); jQuery(“#excerpt”).keyup( function() { jQuery(“#excerpt_counter”).val(jQuery(“#excerpt”).val().length); }); });</script>’; } return; } add_action( ‘admin_head-post.php’, ‘excerpt_count_js’); add_action( … Read more

Posts Page Featured Image [closed]

I feel like such an idiot!! I was trouble-shooting this last night and I guess I removed the featured image for the news page…so, of course, the image wasn’t showing up! I added the featured image and the following code: <?php if(is_home()) { ?> <?php $page_for_posts = get_option( ‘page_for_posts’ ); echo get_the_post_thumbnail($page_for_posts, ‘large’); ?> <?php … Read more

Fastest way to loop through all posts?

“The Loop” is just a name given to the while (have_posts()): the_post(); loop used to iterate over an array of posts returned by WP_Query(). The other function used for querying posts is the get_posts() function, which returns a simple (non-extended) array which you can loop through with a foreach loop. I don’t think it matters … Read more

How to display an icon when a new post is published and then remove it when a specific time past?

Filter the content of post_class(): add_filter( ‘post_class’, function( $classes ) { if ( is_singular() ) return $classes; // now minus last mod time in seconds $diff = time() – mysql2date( ‘U’, $post->post_date ); if ( DAY_IN_SECONDS <= $diff ) $classes[] = ‘new-post’; return $classes; }); Now, in your loop, use post_class(), and you get an … Read more

Custom colors for post rows based on post meta value

If your theme uses wp post class function post_classes($classes) { global $post; $customMetaVariable = get_post_meta( $post->ID, ‘customMetaName’, true ); if($customMetaVariable == ‘desiredCustomMetaValue’){ $classes[] = ‘cssClassName’; return $classes; } } add_filter(‘post_class’, ‘post_classes’); then in your style.css you could use: .cssClassName{ background-color: red; } hence applying that class to all of the posts that contain your desired … Read more

How can I export and then import posts with featured images?

You can consult this answer, but the instructions are a bit unclear, so I’ll clarify them myself here. On the old blog, export the posts using the standard WordPress import/export functionality. If you’re unfamiliar with that, see this link for more information: https://codex.wordpress.org/Importing_Content On the new blog, use the WP Importer to import the XML … Read more