Hot answers tagged

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

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' != ...


8

There is no special hook to author change. But you can achieve it by using post_updated hook. Example: add_action('post_updated', 'prefix_on_update_author', 10, 3); function prefix_on_update_author($post_ID, $post_after, $post_before) { if ($post_after->post_author != $post_before->post_author) { // author has been changed // ...


8

UPDATE 21-01-2016 19:35 SA TIME - BUG FOUND!!!!! YEAH!!!!!! I finally found the bug. As you stated in your last update, the failure only happens when $post_content has a <!--nextpage--> tag in the content. I tested it, and did confirm that any other page after the page after the <!--nextpage--> returns a 404 and then the page gets redirects back ...


7

I tried to figure this out not long ago and the short answer from what I know is no. Not from within the rewrite argument at least. The long explanation becomes apparent if you look at the actual code of register_post_type in wp-includes/post.php line 1454: add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args ); You ...


6

If you view the Media Library in the list mode: /wp-admin/upload.php?mode=list then you will see the Attach/Detach links for each attachment. Each attachment can only be attached to a single parent through the post_parent field in the wp_posst table. Deleting an image from the post editor will not change the post_parent field to 0. Making life a little ...


5

Post ID is always incremental. There's no way the ID of a previous post is assigned to a new post. If a post is deleted, the ID of that post remains vacant forever.


5

Yes. Use $post->guid instead of $post->ID. NB: guid must be in lowercaps for it to work. Detailed explanation here: http://wordpress.stackexchange.com/a/209832/71131 Some additional detail on the purpose of keeping $post->ID as a backup in your third-party service: http://wordpress.stackexchange.com/a/209833/71131


5

Note that there's a syntax error for all those three examples you provided: add_filter('content_pagination', 'custom_content'), 10, 2); add_action('the_post', 'custom_content')); add_action('template_redirect', 'custom_content')); where an extra ) is added. Replace these lines to: add_filter( 'content_pagination', 'custom_content', 10, 2); ...


5

Actually this was quite easy – when you know what's missing: The current post ID for the global $wp_embed object, so it knows what to refer to. The reason is simple: oEmbeds get cached as post meta data, so without knowing the ID, the MarkUp can't get fetched and replaced in the content. // grab a post from the database /** @var \WP_Embed $wp_embed */ ...


5

Say if your have header-video.php used for video post format. Replace get_header(); with get_header(get_post_format()); in your regular singular.php or index.php. If WP can find header-video.php it will load it otherwise it will automatically will fallback to header.php.


5

For the permanent solution, your SQL query is slightly off - you need: UPDATE db1357924680.wp_posts SET post_content = REPLACE( post_content, '[print_me]', '' ) WHERE post_content LIKE '%[print_me]%' MySQL replace example


4

this the default template priority structure for categories: category-slug.php category-ID.php category.php archive.php index.php so if your category slug is "car", you can add file named category-car.php make sure you resave permalinks after adding the file, if it doest work :) more details here


4

I was curious about this and checked the Codex on keyboard shortcuts, but didn't find it mentioned there. I searched and found out that this seems to be already solved, e.g. here and here. I haven't tested out these other plugins so I'm not sure how they solve it, but I decided to take on the challenge and see how this could be solved ;-) So here's my ...


4

New content pagination filter in WordPress 4.4 As of WordPress 4.4 we can use the content_pagination filter ( see ticket #9911 ) /** * Filter the "pages" derived from splitting the post content. * * "Pages" are determined by splitting the post content based on the presence * of `<!-- nextpage -->` tags. * * @since 4.4.0 * * @param array ...


4

You can try following code: get_post_field( 'menu_order', $post_id);


3

A better approach is to address the main search query. @Dipesh's answer is technically correct, but I would suggest using the pre_get_posts action instead to affect the intial query, rather than generating a new one. Much less work! add_action( 'pre_get_posts', 'se39294_search_pre_get_posts' ); function se39294_search_pre_get_posts( $query ) { if ( ...


3

In response to my previous answer: you could of course set the rewrite parameter to false when registering a new post type and handle the rewrite rules yourself like so <?php function wpsx203951_custom_init() { $post_type = 'event'; $args = (object) array( 'public' => true, 'label' => 'Events', 'rewrite' ...


3

All those complex functions can be reduced to one simple function: attachment_url_to_postid() You only need to parse the image URL to retrieve the attachment ID: $attachment_id = attachment_url_to_postid( $image_url ); echo $attachment_id; That's all you need.


3

Your function will never "get" $post as this is not how functions work. Functions cannot pull anything into itself by itself. If you have debug turned on, you will get a definite bug notice that $post in undefined..... You would need to invoke the $post global inside your function for anything to work that relies on the $post global, so the following would ...


3

EDIT - Now that 4.4 is out, you should use the content_pagination filter. See birgire's answer below. You can add formatting to raw post content by applying the content filters directly to $post->post_content: echo apply_filters( 'the_content', $post->post_content ); This will bypass pagination by not using the get_the_content function, which is ...


3

You could use the filter term_links-post_tag to sort tags while ignoring their case: add_filter( "term_links-post_tag", 'themeslug_sort_tags_alphabetically' ); function themeslug_sort_tags_alphabetically( $tags ){ natcasesort( $tags ); return $tags; } This code is to be placed in the functions.php of the child theme.


3

I believe you are talking about single post pages. The template hierarchy does not make provision for single templates according to the category a post belongs to, so single-{$category}.php does not exist. To make single-{$category}.php work, we can make use of the single_template filter add_filter( 'single_template', function ( $template ) { global ...


3

You could use the Metadata API to retrieve the rw_advertising_position metadata for each post, seperate the ads from the content, and then re-insert the ads at the proper locations: /** * Extracts from an array posts with positional metadata and re-inserts them at the proper * indices. See http://wordpress.stackexchange.com/questions/210493 **/ function ...


3

There is almost never a scenario where executing PHP code entered from the wysiwyg editor is a good idea. It opens up a whole bunch of security issues. The best way to achieve what you are looking for is to setup a custom short code that will return the link you are interested in. Add something like this to your functions.php file in your theme. ...


3

You can use the has_password parameter of WP_Query. Here's an example how you can hide it, for non administrators, on the edit.php screen for the post post type: /** * Hide password protected posts, for non-admins, in the case of 'edit-post' screen id * * @link http://wordpress.stackexchange.com/a/200426/26350 */ add_action( 'pre_get_posts', function( ...


3

You can do this with pre_get_posts. This hook is called after the query variable object is created, but before the actual query is run. For excluding category id 32 and 39 from homepage, you can setup a function like this. function wpse_exclude_categories( $query ) { if ( is_admin() ) return; if ( $query->is_home() && ...


3

I'm not going to discuss implementation of a slider, that is too broad. There are a couple of tutorials around which you can look at on how to implement simple sliders The real issue here is the query itself. Sorting a query by the terms a post belongs to is quite heavy operations, and if not done correctly (no offense, as you have done in your answer), it ...


3

There are several ways to deal with this problem. You could have a look for some syntax highlighter plugin, those will also bring you coloured text with highlighted keywords etc. pp. This is probably nice, if you use code quite often. I use SyntaxHighlighter Evolved. If you want to do this by hand, <code> is probably the HTML-element you want to use, ...


3

You can pass variables to javascript using wp_localize_script function: https://codex.wordpress.org/Function_Reference/wp_localize_script Add the following to functions.php if(!function_exists('load_my_script')){ function load_my_script() { global $post; $deps = array('jquery'); $version= '1.0'; $in_footer = true; ...



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