Hot answers tagged

26

I was able to display a 404 error by using the following code in my header. <?php global $wp_query; $wp_query->set_404(); status_header( 404 ); get_template_part( 404 ); exit(); ?> To break it down: $wp_query->set_404(): tells the wp_query this is a 404, this changes the title status_header(): sends a HTTP 404 header get_template_part()...


19

I'm assuming that you put WordPress in your site root and the external directories are also in your site root. The reason this is happening is that .htaccess files follow a hierarchy. Whatever directives are in the top-level .htaccess file flow down and apply to all directories below it. If this is the case, you can do one of several things: Move your ...


18

After a bit more slogging through code and Googling, I found the answer. It's contained in this thread (see Otto42's post), but for the record, adding the following to your plugin will override the 404 handling for the conditions you specify: add_filter('template_redirect', 'my_404_override' ); function my_404_override() { global $wp_query; if (<...


16

You could try the Wordpress function status_header() to add the HTTP/1.1 404 Not Found header; So your Code 2 example would be: function rr_404_my_event() { global $post; if ( is_singular( 'event' ) && !rr_event_should_be_available( $post->ID ) ) { global $wp_query; $wp_query->set_404(); status_header(404); } } add_action( '...


12

I could be wrong, but I vaguely remember that: name, email get hijacked by WordPress to do post comments, if you renamed the form elements to be contact-name and contact-email, do you get the same issue?


12

This has worked for me in the past for a similar situation : Put this on top of .htaccess ErrorDocument 401 default


12

I would use the wp_title filter hook: function theme_slug_filter_wp_title( $title ) { if ( is_404() ) { $title = 'ADD 404 TITLE TEXT HERE'; } // You can do other filtering here, or // just return $title return $title; } // Hook into wp_title filter hook add_filter( 'wp_title', 'theme_slug_filter_wp_title' ); This will play ...


12

This is my first time participating here on Stack Exchange, but I'll give this a go and see if I can help point you in the right direction. By default, hierarchical CPTs behave exactly the way you've described. The unique slug prefix, "newsletter" in this case, is to let the rewrite engine know how to tell requests for different post types apart. Those CPT ...


9

You may be surprised, but there is nothing strange there. First of all let's clarify that in WordPress when you visit a frontend URL you trigger a query. Always. That query is just a standard WP_Query, just like the ones run via: $query = new WP_Query( $args ); There is only one difference: the $args variables are generated by WordPress using the WP::...


8

This code worked for me: add_action( 'wp', 'force_404' ); function force_404() { global $wp_query; //$posts (if required) if(is_page()){ // your condition status_header( 404 ); nocache_headers(); include( get_query_template( '404' ) ); die(); } }


7

There is an action specifically for this: function my_parse_query( $wp_query ) { if ( $wp_query->get( 'my_custom_var' ) > 42 ) { $wp_query->set_404(); status_header( 404 ); } } add_action( 'parse_query', 'my_parse_query' ); That should load the 404.php template in your theme, if you have it. Otherwise, it will fall back to ...


7

It's bad practice to do so purely because the Search Results page was found it just returned no results. A 404 would be used if the search page didn't exist.


7

get_the_ID() is broken. function get_the_ID() { return get_post()->ID; } It tries to use the member ID on a function that returns a post object sometimes: /* * @return WP_Post|null WP_Post on success or null on failure */ function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { get_posts() can return NULL, and NULL has no ...


6

Try flushing your mod_rewrite rules: Dashboard -> Settings -> Permalinks Save settings (no need to make any changes)


6

The posts_per_page refers to how many posts per page, and not what page you are viewing. For that, you want to set the paged argument. See the Codex on WP_Query and this article by Scribu. With the paged argument set, it'll return the appropriate posts depending on the page number (the first x posts for page 1, the next x posts for page 2 etc). If not, you ...


6

Put <?php if( !is_404() ) : ?> before your navigation and <?php endif; ?> after your navigation and VIOLA! It's gone!


6

Error pages are served up via .HTACCESS, if you are using Apache you would use the ErrorDocument directive and add the status and URL to it. So it would look like this in your .htaccess file: ErrorDocument 401 http://yourwebsite.com/error-401 ErrorDocument 403 http://yourwebsite.com/error-403 ErrorDocument 500 http://yourwebsite.com/error-500 You could ...


5

Problem solved. The plugin "User Access Manager" was found guilty of inserting a .htaccess file into wp-content/uploads/ and not handling calls properly afterwards. I don't know how UAM plugin could be fixed, but It's ok to remove the .htaccess file. Nothing else depends on it. (at least in my case)


5

Martin's fix works, but a better solution is to use the pre_get_posts function. Example: function custom_type_archive_display($query) { if (is_post_type_archive('custom_type')) { $query->set('posts_per_page',1); return; } } add_action('pre_get_posts', 'custom_type_archive_display');


5

I founded the solution here > http://walrusinacanoe.com/web-development/742 Is elegant and functional> add_action( 'parse_query','changept' ); function changept() { if( is_category() && !is_admin() ) set_query_var( 'post_type', array( 'post', 'your_custom_type' ) ); return; }


5

Why not do a one-to-one redirect? When migrating the data over, add a custom field like '_legacy_url' in the WXR or whatever mechanism you're using to track the old URLs. Then, check during the template_redirect action for a one-to-one match whenever WP throws a 404. add_action('template_redirect', 'typepad_redirects', 0); function typepad_redirects(){ ...


5

You should set your publicly_queryable argument to true when registering your custom post type. TAKE NOTE: Add flush_rewrite_rules(), refresh the page once or twice and REMOVE IT IMMEDIATELY. You SHOULD NOT keep flush_rewrite_rules() unless under the provisions as in the codex. this is an expensive operation so it should only be used when absolutely ...


4

The only way to debug this is to disable one plugin at a time, each time trying to reproduce the problem before you disable another plugin. Start with the plugins that have anything to do with the administration of WP, then move down to regular theme plugins, widgets and such. Inspect the "Not Found" page that you are served better (browse with Opera and ...


4

I needed to do the same for a custom project where there was always a 200 page, and found you can also simply add this to the top of your template file ( above get_header(); ) global $wp_query; status_header( 200 ); $wp_query->is_404=false;


4

I guess you get http://yoursite but http://yoursite/anything I had a similar problem and it got resolved when I changed the httpd.conf from AllowOverride None to AllowOverride All As per my understanding it will make the .htaccess take effect.


4

Yea, you need to flush the permalinks. This helps solve the problem even further. 'rewrite' => array("slug" => "products"), // Permalinks format I was getting the same problem when using 'rewrite' => true,


4

This is now working for me... for those of you having the same issue it turns out all the code was correct. The problem was WordPress is setup to show 10 posts per page by default which clashed with my query (limiting it to 2 posts) to fix the issue I changed the WordPress setting (Settings / Reading in the admin dashboard) to 1.


4

.htaccess skip WordPress 404 error handling for static files. <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(robots\.txt|sitemap\.xml(\.gz)?) RewriteCond %{REQUEST_FILENAME} \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|...


4

If you just visit your permalinks setting page, it'll flush the rewrite rules as well.



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