Show all posts in sidebar in single.php

Initiating new WP_Query($post) shows all posts. Credit to @Latheesh V M Villa Add: $loop_query = new WP_Query($post) Then change loop values like: have_posts() to: $loop_query->have_posts() loop.php <?php $loop_query = new WP_Query($post); if ($loop_query->have_posts()): while ($loop_query->have_posts()) : $loop_query->the_post(); ?> <article class=”loop-template” id=”post-<?php the_ID(); ?>” <?php post_class(); ?>> <!– article content here –> </article> <?php endwhile; ?> … Read more

How to automatically apply a category based on the post title?

You could try this. I havn’t really tested it, so I’m not sure it works – might require some small tweaks aswell. function add_category_based_on_title( $post_id ) { // title $title = get_the_title( $post_id ); // get first part of title $substr = explode( ” “, $title )[0]; // get category id $category_id = get_cat_ID( $substr … Read more

Using The REST API How To Pull All Custom Posts?

The filter is dynamic, i.e. generated from rest_{$post_type}_query. So for the post post type it’s rest_post_query and for the cpt post type it’s rest_cpt_query Note that REST pagination is available: https://example.com/wp-json/wp/v2/posts?per_page=100&page=1 https://example.com/wp-json/wp/v2/posts?per_page=100&page=2 … The maximum of per_page is to reduce the risk of fatal errors if available resources are exceeded when too many items are … Read more

Restrict next_post_link() to current category

OK, so there’s a problem with Codes in this part, I guess. The Codex article on Next and Previous Links contradicts the article specifically on next_post_link() In that article you can clearly see, that it looked a little bit different. In the part describing next_post_link there is a note: Deprecated: previous_post() and next_post(). Use: –> … Read more

Define a wordpress constant through plugin functions?

I don’t think you should define constants in your plugin. It will be very hard to debug later on. IMHO using wp_revisions_to_keep filter will be much nicer solution. So your code could look like this: add_filter( ‘wp_revisions_to_keep’, ‘my_revisions_to_keep_based_on_settings’, 10, 2 ); function my_revisions_to_keep_based_on_settings( $num, $post ) { // change that according to your needs return … Read more

Add ajax load more function using WP API

I’ve solved the problem, I was made a mistake with the variable pull_page that is inside the click event. I’ve moved the variable outside the event and now all works fine. Here is the updated code! var pull_page = 1; var jsonFlag = true; $(‘#load-more’).on(‘click’, function(e){ e.preventDefault(); if(jsonFlag){ pull_page++; $.getJSON(“/u/wp-json/portfolio/all-posts?page=” + pull_page, function(data){ if(data.length){ pull_page++; … Read more

Hiding Posts on Front Page and from all plugins

If you want to hide certain posts from all queries on the front page, then remove the conditions you’re using and check is_front_page() instead: function exclude_single_posts_home($query) { if (is_front_page()) { $query->set(‘post__not_in’, array(69489,69177,68878,68736)); } } Normally checking the global state is the incorrect way to filter queries (what you’d originally done is the normal correct way), … Read more

View the number of entries recorded per day

The easiest way is by using custom MySQL query with WPDB class. global $wpdb; $table = $wpdb->prefix . ‘posts’; $sql = “SELECT DATE(post_date) AS date, COUNT(ID) AS count FROM {$table} WHERE post_type=”my_post_type” AND post_status=”publish” GROUP BY DATE(post_date)”; $rows = $wpdb->get_results($sql); Or if you want to prevent SQL injection, you can use prepared statement like this: … Read more

Copying and pasting emoji in a wordpress post

Emoji are just letters. They’re defined by the user’s operating system. The same way the alphabet is. You don’t need to do anything special. If you did, then that website would be pointless. The only thing you need to consider is older operating systems that don’t support Emoji, but WordPress itself loads a script that … Read more