Hot answers tagged

75

Simple as it gets $my_postid = 12;//This is page id or post id $content_post = get_post($my_postid); $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;


63

echo get_post_field('post_content', $post_id);


28

here's what we did yesterday on a client site that we were having this exact problem with... I created a quick filter as a plugin and activated it. <?php /* Plugin Name: Image P tag remover Description: Plugin to remove p tags from around images in content outputting, after WP autop filter has added them. (oh the irony) Version: 1.0 Author: Fublo Ltd ...


26

If you read the documentation for wp_insert_post, it returns the post ID of the post you just created. If you combine that with the following function __update_post_meta (a custom function I acquired from this site and adapted a bit) /** * Updates post meta for a post. It also automatically deletes or adds the value to field_name if specified * * @...


26

Use the page_for_posts option: <?php $page_for_posts = get_option( 'page_for_posts' ); ?> This will return the ID of the Page assigned to display the Blog Posts Index.


21

Massively improved function developed for plugin heavy on images: if ( ! function_exists( 'get_attachment_id' ) ) { /** * Get the Attachment ID for a given image URL. * * @link http://wordpress.stackexchange.com/a/7094 * * @param string $url * * @return boolean|integer */ function get_attachment_id( $url ) {...


19

I believe the best way to do this in a plugin is to run the following sample function when the pre_get_posts action hook is encountered. The $wp_query object is available, meaning your conditional tags are available, but before WordPress gets the posts, which means you are changing query vars prior to the first query being run, rather than adding a second ...


19

is_home() checks for the "Posts Page", despite the somewhat confusing function name.


17

function my_manage_columns( $columns ) { unset($columns['date']); return $columns; } function my_column_init() { add_filter( 'manage_posts_columns' , 'my_manage_columns' ); } add_action( 'admin_init' , 'my_column_init' );


17

Create a file install.php in your wp-content directory. In that file you declare the wp_install_defaults() function. If you just leave that function empty no default categories, links, posts or pages will be created. The default (pluggable) wp_install_defaults() function can be found in wp-admin/includes/upgrade.php. Also see: http://stackoverflow.com/...


17

The following code will work, but you just have to keep in mind that conflicts can happen easily if the slug for your custom post type is the same as a page or post's slug... First, we will remove the slug from the permalink: function na_remove_slug( $post_link, $post, $leavename ) { if ( 'events' != $post->post_type || 'publish' != $post->...


16

Every post update in WordPress is handled by the wp_update_post function. This function has some defaults, and for post_content_filtered the default value is '' (empty string). Once the defaults are merged with args passed to function via wp_parse_args it means that every time a post is updated and post_content_filtered is not explicitly passed, it is set ...


15

If you are talking about the answer i posted here its simply uploading file in an iframe to achieve "Ajax like" submit. Now if you already have a form that handles the post submit you can simply add the upload file field input somewhere in your form: <form ... ... <input type="file" name="thumbnail" id="thumbnail"> ... ... </form> make ...


15

Updating the posts $all_posts = get_posts( 'posts_per_page' => -1, 'post_type' => 'post' ); foreach ( $all_posts as $single ) { wp_update_post( array( 'ID' => $single->ID, 'post_title' => to_title_case( $single->post_title ) // see function below )); } Converting a string to "Title Case" And, while not ...


14

The answer by shea is not ideal as in many cases: You don't want to strip everything from <br>, <p> etc. You want it as a default behavior for your WP visual composer which the above code will delete In many cases it is considered as "hacking the core" as this is changing the default core behavior of WP - for example such a thing will not pass ...


14

I don't think there exist one, but you could create your own, wpse_empty_trash, with something like this: /** * Add a custom hook 'wpse_empty_trash' */ add_action( 'load-edit.php', function() { add_action( 'before_delete_post', function ( $post_id ) { if ( 'trash' === get_post_status( $post_id ) && ...


13

Basically you need to make WordPress treat img like block-level element for the purpose of formatting. Such elements are hardcoded in wpautop() and list is unfortunately not filtered. What I would do is: Fork wpautop() under different name. Add img to regexp in $allblocks variable. Remove wpautop from the_content filter. Add your forked version to ...


13

When updating post taxonomies, you can call wp_defer_term_counting( true ) before you do your mass operation, and then wp_defer_term_counting( false ) to update the term counts only once per term.


13

Hi @Robin I. Knight: I view get_the_excerpt() as a function with legacy design. As WordPress usage has grown there are many newer use-cases where it doesn't fit but where the newer functions for getting different data do. One example is the now frequent use of an $args array of function options. But it's easy to fix for your needs. Here's an alternative ...


12

assuming you're using post_class(): add_filter('post_class', function($classes){ global $wp_query; if(($wp_query->current_post + 1) == $wp_query->post_count) $classes[] = 'last'; return $classes; });


12

The mechanics of excerpt are extremely confusing. It is not precise answer to your question but in general if you need to make template tags, specific to Loop, work with array returned by get_posts() you can emulate Loop like this: $stories = get_posts(); foreach ($stories as $post) { setup_postdata($post); // stuff } wp_reset_postdata();


12

You can add an endpoint to your URIs to handle special requests. Here is a basic example as plugin. To understand what's going on read Christopher Davis's fantastic tutorial A (Mostly) Complete Guide to the WordPress Rewrite API. <?php # -*- coding: utf-8 -*- /** * Plugin Name: T5 Endpoint Example * Description: Adds a permalink endpoint to posts ...


12

You can use the following instead, if (is_singular('post')) { //your code here... } Where by is_singular is the WordPress API conditional function for testing for the existence of a post type. You can also pass an array of post types if you wish. http://codex.wordpress.org/Function_Reference/is_singular


12

Although I do not understand the motivation for truncating posts, I think this exercise is valuable for you to understand how to use cron + WordPress. Create a function to truncate posts This can be used for both methods below WP-cron or UNIX cron. function foobar_truncate_posts(){ global $wpdb; # Set your threshold of max posts and post_type ...


12

First you have to get the images. How to get all images of a gallery is described here. WordPress uses two classes for unzipping files. The PHP bilt in ZipArchive() (usage see David Walsh). And PclZip, you can found this class in wp-admin/includes/class-pclzip.php. If you got problems with ZipArchive() try the PclZip class. Now you just to have to glue ...


12

There is a sad truth: you can never ever be sure that some code will not break your code, and there is nothing you can do to prevent that. Especially in WordPress, where everything is global. That said, yes, global $post is one of the most used global var, so using special care for it can be a good idea. In my code I rarely directly access global $post. ...


11

Hi Scott B: Hate to be the bearer of bad news but WordPress hardcodes the Page Template functionality to the "page" post type, at least in v3.0 (that might change in future versions but there's not a specific initiative I'm aware of to change it yet. So this is one of the very few times I'm struggling to figure out how to get around something without ...


11

<?php $postTitle = $_POST['post_title']; $post = $_POST['post']; $submit = $_POST['submit']; if(isset($submit)){ global $user_ID; $new_post = array( 'post_title' => $postTitle, 'post_content' => $post, 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => $user_ID,...


11

It seems that the easiest way is to do it manually in the DB. If you don't have access to the DB, or if you don't want to do it manually, you can install a plugin that will do it one post at a time, or a plugin that will bulk convert a lot of posts at once


11

if ($wp_query->current_post +1 == $wp_query->post_count) { // this is the last post } Change $wp_query to your own query variable if you made a new WP_Query object.



Only top voted, non community-wiki answers of a minimum length are eligible