Hot answers tagged

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->...


8

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 ...


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 ...


6

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


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 ...


6

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


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

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

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

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); add_action(...


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

There is no need to do an overrated amount of queries to accomplish this. you can still use only one query, the_posts filter and some PHP to sort your code as needed. I take that this is a custom query from what I read in your question, so we can do the following: First, we want to introduce a custom WP_Query parameter so that we can use that parameter ...


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

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

There is an easier way to do this: add_filter( 'the_content', 'my_post_content_remove_shortcodes', 0 ); function my_post_content_remove_shortcodes( $content ) { /* Create an array of all the shortcode tags. */ $shortcode_tags = array( 'shortcode_1', 'shortcode_2', 'shortcode_3' ); /* Loop through the shortcodes and ...


4

Since you are already getting the ID of the newly created post ($post_id), you just need to get the URL and redirect after the submission. Using get_permalink( $post_id ) you can get post's URL. Also, you can get rid of the header() function to send a new HTTP header, and use WordPress' own function for redirect - wp_redirect(). wp_redirect( ...


4

You can use the_post hook to remove <!--nextpage-->. In this case: add_action( 'the_post', 'campaign_remove_nextpage', 99); function campaign_remove_nextpage ( $post ) { if ( ($_GET['utm_campaign']== 'nonextpagecampaign') && (false !== strpos( $post->post_content, '<!--nextpage-->' )) ) { // Reset the global $pages: ...


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

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() && $query-&...


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

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

The correct method of doing this since Wordpress 4.4 is simply the following: add_filter( "get_next_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){ $where .= " AND p.post_author='".$post->post_author."'"; return $where; }, 10, 5); add_filter( "get_previous_post_where", function($where, $in_same_term, $excluded_terms, ...


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

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

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

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