How to get a list of all the years that have posts

I am not sure why you prefer the longer method over wp_get_archives(). I recommend letting WordPress do the work for you. It is more efficient, and easier to read. However … since you presented a particular preference: It would appear that you are using an array for post_type, when none is required. Try modifying your … Read more

How to get 2 or multiple custom post types in wordpress functions.php

In your if statement just add more conditions like this. function et_last_modified_date_blog( $the_date ) { if ( ‘post’ === get_post_type() || ‘movies’ === get_post_type() || ‘etc’ === get_post_type()) { $the_time = get_post_time( ‘His’ ); $the_modified = get_post_modified_time( ‘His’ ); $last_modified = sprintf( __( ‘Last updated %s’, ‘Divi’ ), esc_html( get_post_modified_time( ‘M j, Y’ ) ) … Read more

Invisible spam post in backend

I’d try open the spam post in your browser and find the post ID. This will be in a rel=”shortlink” tag in the HTML header: <link rel=”shortlink” href=”https://example.com/?p=123″ /> or as a class page-id-123 or similar on the body tag: <body class=”page-template-default page page-id-123 logged-in admin-bar no-customize-support”> edit that ID into your normal edit post … Read more

Remove slashes (both before and after) in relative post url

It’s because you’re not actually putting the result of ltrim() and rtrim() back into the variable. Those functions return the trimmed value, they don’t modify the passed variable. So you need to do this: $post_url_rel = wp_make_link_relative(get_permalink( $post_id )); $post_url_rel = ltrim($post_url_rel, “https://wordpress.stackexchange.com/”); $post_url_rel = rtrim($post_url_rel, “https://wordpress.stackexchange.com/”); Or better yet, just use trim(), which will … Read more

Display a post by ID attribute with shortcode

Try $HTML instead of $html Linux based systems are case-sensitive … function post_banner_shortcode($atts) { $atts = shortcode_atts( array( ‘id’ => ” ), $atts ); $post_id = $atts[‘id’]; $HTML = ‘<div class=”postbanner”>’; $HTML .= ‘<div class=”pb-thumb”>’ . get_the_post_thumbnail($post_id, ‘medium’) . ‘</div>’; $HTML .= ‘<div class=”pb-ttl-txt”>’; $HTML .= ‘<h4>’ . get_the_title($post_id) . ‘</h4>’; $HTML .= ‘<p>’ . … Read more

Templating of a specific post ID

You have to follow this file name structure: single-{post-type}-{slug}.php If you have a post names “Hello World” and the slug of post is hello-world then the template name will be single-post-hello-world.php Update: There is no default way to use id in template name. But you might try this snipped: <?php add_filter( ‘single_template’, ‘load_post_template_by_id’ ); function … Read more

Limit Gutenberg blocks available to users to choose from

In the functions.php file I added add_filter( ‘allowed_block_types_all’, ‘func_allowed_block_types’ ); function func_allowed_block_types( $allowed_blocks ) { return array( ‘core/embed’ ); } Then in my plugin I added this JS to the javascript file to enable only the embed blocks I wanted (Twitter, youTube and Vimeo) wp.domReady( function() { const allowedEmbedBlocks = [ ‘twitter’,’youtube’, ‘vimeo’ ]; wp.blocks.getBlockType( … Read more

Exclude posts w/ specific post_meta on Rest API endpoint

Caveat From the REST API docs: Changing or removing data from core REST API endpoint responses can break plugins or WordPress core behavior, and should be avoided wherever possible. That said, there’s a rest_post_query filter (well, rest_{$this->post_type}_query) that will let you modify the WP_Query that’s grabbing your posts, so you should be able to add … Read more