Exclude posts with empty post_content in wp_query

This isn’t possible with a standard WP query, and you’ll have to leverage the use of posts_where before the WP_Query is called. function the_dramatist_filter_where($where=””){ $where .= ” AND trim(coalesce(post_content, ”)) <>””; return $where; } In the above, we’re simply only selecting posts where the column post_content isn’t empty. Then add the filter. add_filter(‘posts_where’, ‘the_dramatist_filter_where’); Now … Read more

How to restore permanently trash page? [closed]

The only way to restore it is to restore a backup of your MySql database. Login to your web host and see if there are any backups of your database available. Then you can restore the backup and it will bring that content back. Make sure you haven’t added any other content after the backup … Read more

How can I create custom button in post.php

Use media_buttons action for it. Check below code. add_action(‘media_buttons’, ‘add_my_media_button’); function add_my_media_button(){ echo ‘<a href=”‘.get_site_url().’/wp-admin/customize.php” class=”button button-default”>Customize</a>’; } Hope this will helps you. Thanks!

Add container div to gallery using post_gallery

Adding an HTML wrapper to the gallery can be done using the post_gallery filter. Here’s a fully commented example. add_filter( ‘post_gallery’, ‘gallery_custom’, 10, 3 ); /** * Filters the default gallery shortcode output. * * If the filtered output isn’t empty, it will be used instead of generating * the default gallery template. * * … Read more

How to run a function when post is edited or updated using publish post action?

With the post_updated hook you can trigger an action when the post is updated. He passes 3 parameters: $post_ID (the post ID), $post_after(the post object after the edit), $post_before (the post object before the edit) Here’s an example: <?php function check_values($post_ID, $post_after, $post_before){ echo ‘Post ID:’; var_dump($post_ID); echo ‘Post Object AFTER update:’; var_dump($post_after); echo ‘Post … Read more

Will post_exists work for draft posts?

Yes, as post_exists(“great title”) runs the following SQL: SELECT ID FROM $wpdb->posts WHERE 1=1 AND post_title=”great title” so there’s no post status or post type restriction. Check the docs on post_exists() for more info. If you want to restrict by post type, you can use: $found_page = get_page_by_title( “great title”, $output = OBJECT, $post_type=”page” ); … Read more

WP Query related posts by current page Tag ID

get_the_tags() returns an array of tags name, ID, and more. You should store only IDs in an array, and use it in your query. $post_tag = get_the_tags ( $post->ID ); // Define an empty array $ids = array(); // Check if the post has any tags if ( $post_tag ) { foreach ( $post_tag as … Read more

get_previous_post not working as expected

Depending on context, you may need global $post; first, so you’re not operating on a different local var. You also already have a post object, so you could just assign that directly to $post before calling setup_postdata… However, you can skip all that and use functions that accept a post object argument- $prevPost = get_previous_post(true); … Read more

if I create ‘front-page.php’, then how do I link to post index?

Since WordPress 4.5 you can use get_post_type_archive_link( ‘post’ ) to link to the page containing the blog posts. Depending on what you’ve set under Settings -> Reading, this will be either the front page, e.g. example.com/ or a specific page like example.com/news/. home_url( “https://wordpress.stackexchange.com/” ) always points to the front page.