Display Posts by Categories

Try this code <div class=”container”> <!– Example row of columns –> <div class=”row front-posts”> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <div class=”line2″></div> <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query(); ?> <!– Start latest post –> <div class=”line2″> <?php $paged = get_query_var(‘paged’) ? get_query_var(‘paged’) : 1; … Read more

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