Tag Info

Hot answers tagged

21

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


15

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


14

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


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


11

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


11

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?


10

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


10

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


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 to run via: $query = new WP_Query( $args ); There is only one difference: the $args variable are generated by WordPress using the ...


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

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.


6

There is a method 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 ...


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

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(){ ...


4

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; }


4

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(); } }


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

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


4

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


4

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


4

You don't need to add anything special to the top of 404.php. WordPress will know to use 404.php automatically when it tries to get a post or page and fails. To create a custom 404 page for a theme, the simplest way is to: Copy the index.php file from the current theme to a file called 404.php. Edit your new 404.php to delete the code dealing with ...


4

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

By the time you reach the template, WordPress has already queried the database and decided what to display based on those results. You're seeing a 404 error because based on the default main query, there are no more posts to show. When you call query_posts in the template, you overwrite that original query. Despite the fact that your new query results ...


4

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');


3

Step 1, add the rewrite tags for custom event year and month query vars, then register the event post type with those tags in the slug argument of the rewrite argument: function wpa83531_register_event_post_type(){ add_rewrite_tag('%event_year%','(\d+)'); add_rewrite_tag('%event_month%','(.+)'); register_post_type( 'event', array( ...


3

This is challenging, because showing something would require page not to be private. It is more common to leave page public, but make it produce conditional output depending on if user is logged (is_user_logged_in()) in or other criteria.


3

Creating a secondary query or overwriting the main query inside a page template is the quickest and easiest way I know to break pagination. The main query, which determines which page to load runs before your template thus the results on the page and the query that loads the page become out of sync. The main query does not know about your in-template ...


3

Headers are sent long before you try to alter them. Headers are sent by actions associated with get_header(), so by the time your code executes, it is too late to alter the headers. You can demonstrate this with a simple experiment. Try each of the following: get_header(); status_header( 404 ); and status_header( 404 ); get_header(); In a template ...



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