single- and archive- templates not being applied to custom post types, not even after flushing permalinks

Answering my own question here, for anyone who might encounter this problem in the future. The cause for the right templates not loading was having uppercase letters in my template filename. Changing all characters in the filename to lowercase resolved the problem, and the templates are now being applied as they should. I’m still unsure … Read more

Add new post only in assigned category

First remove the category metabox from the post edit screen for this particular role, let’s name it onecat-author: if (current_user_can(‘onecat-author’)) { remove_meta_box (‘category’, ‘post’, ‘normal’); } Next, when the post is saved use the available hook to assign the category: if (current_user_can(‘onecat-author’)) { add_action (‘save_post’, ‘wpse_231846_save_cat’, 10, 1 ); } function wpse_231846_save_cat ($post_id) { wp_set_post_categories … Read more

Only show related posts when another post has same term

You can and should do it in a more simple way: In this example you can see the $tags variable, it get the tags of the post compare them and shows 7 posts (the showposts parameter) also makes sure that it won’t show the original post (post__not_in parameter). <?php // related posts based on first … Read more

Shortcode: How to add custom posts?

Correct. post__in requires an array but the id parameter in [video-course limit=8 id=”418, 1186″] is passed in as a string, so in your shortcode, you would have to parse it into an array in order to use it: if ( !is_array($id) ) { // If $id is not an array (not your default value in … Read more

Get the category of post

Just realised what the problem is, the code should basically be: <?php $catergory = get_the_category(); ?> <?php echo $catergory[0]->name; ?> I think the different versions of PHP from localhost / server were different which is why it was working on one but not the other (feel free to correct me if I’m wrong)

How to test if user is filtering post list in dashboard

I have found a solution, using $query->get(‘cat’): when restricted users select “all categories” $query->get(‘cat’) will return ‘0’, so in this case I will apply filtering, otherwise my code won’t run, allowing user-selected filter (in a restricted range) to be applied /********** post & media lists filtered by user role & category **************** function posts_for_current_role($query) { … Read more

Repeating post how to solve this

I found the solution. I remove this $paged = (get_query_var(‘page‌​’)) ? get_query_var(‘page’‌​) : 1; from the query now its working fine without repeating the posts.

Can’t get post_updated hook to work

If it doesn’t work you can try to use a wp_insert_post action: function insert_post_hook($post_id, $post) { if ($post->post_date != $post->post_modified) { // This post is being updated! } else { // This post is being created! } } add_action( ‘wp_insert_post’, ‘insert_post_hook’, 10, 2 ); If you want to see the latest version you could enable … Read more

Keep highlight on menu item with post pages

Yes! Found a solution which is working fine for my case 🙂 if(window.location.href.indexOf(“/blog/”) > -1) { $(‘#menu-item-339’).addClass(‘current-menu-item’); } if(window.location.href.indexOf(“/articles/”) > -1) { $(‘#menu-item-241’).addClass(‘current-menu-item’); } if(window.location.href.indexOf(“/forum/”) > -1) { $(‘#menu-item-437’).addClass(‘current-menu-item’); } Solution found here