Custom post type won't display, IS homepage & main query - wordpress

I have a custom post type that worked just fine earlier, but not anymore. I haven't installed any new plugins and my code seems perfectly valid. Other custom fields are showing (default post type), but the ones I'm talking about not anymore.
Here's the code:
// Show posts of 'post', 'homepage_slider' post types on home page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}
As you can guess, homepage_slider is my Custom Post Type. I haven't tweaked it's code one bit, but here it is just for reference:
function homepage_slider() {
$labels = array(
'name' => 'Images',
'singular_name' => 'Image',
'menu_name' => 'Homepage Slider Images',
'parent_item_colon' => 'Parent Image:',
'all_items' => 'All images',
'view_item' => 'View Image',
'add_new_item' => 'Add New Image',
'add_new' => 'Add New',
'edit_item' => 'Edit Image',
'update_item' => 'Update Image',
'search_items' => 'Search Image',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
);
$args = array(
'label' => 'homepage_slider',
'description' => 'Homepage Slider',
'labels' => $labels,
'supports' => array( 'title', 'thumbnail' ),
// 'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'myurl',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type( 'homepage_slider', $args );
}
// Hook into the 'init' action
add_action( 'init', 'homepage_slider', 0 );
I have queried both the fact that it is indeed the homepage and the main query is indeed running. Really weird error.
Any suggestions?
EDIT: I think something's wrong with the add_my_post_types_to_query function as it's not working with another Custom Post Type either.

Try replacing:
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}
With:
function add_my_post_types_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}

Okay, so here's how I fixed it. Not quite a natural fix, but it did the job.
I simply created a new loop that runs through the custom post type wherever it must be placed and repeat for every other custom field I have on the homepage.
:)

Related

Ordering custom post types

I'm registering a new custom post type like this:
function news_register() {
$labels = array(
'name' => _x('News', 'post type general name'),
'singular_name' => _x('News Item', 'post type singular name'),
'add_new' => _x('Add New News Article', 'resources item'),
'add_new_item' => __('Add New News Article'),
'edit_item' => __('Edit News Article'),
'new_item' => __('New News Article'),
'view_item' => __('View News Article'),
'search_items' => __('Search News Articles'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 8,
'supports' => array('title','editor', 'excerpt', 'thumbnail', 'excerpt')
);
register_post_type( 'news' , $args );
}
add_action('init', 'news_register');
And I'm then trying to query those post types and order them by title using:
$news = new WP_Query(array(
'post_type' => 'news',
'orderby' => 'name',
'order' => 'DESC'
));
Or:
$args = array( 'post_type' => 'news', 'posts_per_page'=>5, 'orderby'=>'title','order'=>'ASC');
$news = new WP_Query( $args );
Neither of which order by the post title at all. The news posts are test articles that are called 'aaa', 'bbb' and 'ccc.
How can I change my query to get the posts by title order?
EDIT:
I have this code in functions, but removing it doesn't seem to change anything.
add_filter( 'pre_get_posts','change_my_post_order' );
function change_my_post_order( $query ) {
global $wp_query;
if ( is_category() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'asc' );
}
return $query;
}
Adding "'suppress_filters' => true," to the query doesn't seem to make any difference either.
Your pre_get_posts action is wrong. You should make sure that you only target the front end and only the main query. Your action should look like this
add_action( 'pre_get_posts','change_my_post_order' );
function change_my_post_order( $query ) {
if ( !is_admin() && $query->is_main_query() && $query->is_category() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'asc' );
}
}

Wordpress Custom Type permalink with custom Taxonomy slug

I have a custom post type on my site called Homes, with a custom taxonomy called homes-category. They are both working correctly, but I am having trouble with the permalinks. I need the permalink to be homes/homes-category/pagename. I wrote a rewrite function, so the permalink is showing up correctly whenever I go to a homes item, but I get a 404 on the page itself. I am not sure what is causing this and have no ideas on how to fix it. The homes items work fine without the custom taxonomy in the permalink, but not with it. Does anyone have any ideas on how to fix this? I've been searching for days with no luck.
Here is the code for my custom post type:
register_post_type( 'Homes',
array(
'labels' => array(
'name' => __( 'Homes' ),
'singular_name' => __( 'Homes Item' ),
'add_new_item' => __('Add New Homes Item'),
'edit_item' => __('Edit Homes Item'),
'new_item' => __('New Homes Item'),
),
'supports' => array('title', 'thumbnail', 'editor'),
'taxonomies' => array('homes-category'),
'public' => true,
'has_archive' => false,
'show_in_nav_menus' => TRUE,
'show_in_menu' => TRUE,
'rewrite' => array(
'slug' => 'homes/%homes-category%',
'with_front' => true,
'hierarchical' => true,
),
)
);
Here is the code for my custom taxonomy
register_taxonomy(
'homes-category',
'homes',
array(
'hierarchical' => true,
'label' => __( 'Availability Category' ),
'rewrite' => array(
'slug' => 'homes',
'with_front' => false,
),
)
);
And here is the rewrite function
function custom_post_link($post_link, $id = 0)
{
$post = get_post($id);
if(!is_object($post) || $post->post_type != 'homes')
{
return $post_link;
}
$homes = 'misc';
if($terms = wp_get_object_terms($post->ID, 'homes-category'))
{
$client = $terms[0]->slug;
//Replace the query var surrounded by % with the slug of
//the first taxonomy it belongs to.
return str_replace('%homes-category%', $homes, $post_link);
}
//If all else fails, just return the $post_link.
return $post_link;
}
add_filter('post_type_link', 'custom_post_link', 1, 3);
You need to add rewrite_tag for, '%homes-category%'
Even you need to add, custom rewrite_rule, to interpret it correctly.
You can use below code,
add_action('init','wdm_register_post_types');
function wdm_register_post_types()
{
global $wp_rewrite;
register_post_type( 'Homes',
array(
'labels' => array(
'name' => __( 'Homes' ),
'singular_name' => __( 'Homes Item' ),
'add_new_item' => __('Add New Homes Item'),
'edit_item' => __('Edit Homes Item'),
'new_item' => __('New Homes Item'),
),
'supports' => array('title', 'thumbnail', 'editor'),
'taxonomies' => array('homes-category'),
'public' => true,
'has_archive' => false,
'show_in_nav_menus' => TRUE,
'show_in_menu' => TRUE,
'rewrite' => array(
'slug' => 'homes/%homes-cat%',
'with_front' => true,
'hierarchical' => true
),
)
);
register_taxonomy(
'homes-category',
'homes',
array(
'hierarchical' => true,
'label' => __( 'Availability Category' ),
'rewrite' => array(
'slug' => 'homes-category',
'with_front' => true,
),
)
);
$wp_rewrite->add_rewrite_tag( '%homes-cat%', '[a-zA-Z0-9_]');
add_rewrite_rule('^homes/([^/]+)/([^/]+)/?$','index.php?homes=$matches[2]','top');
}
function wp_tuts_filter_post_link( $permalink, $post ) {
if ( false === strpos( $permalink, '%homes-cat%' ) )
return $permalink;
$terms = current(wp_get_post_terms( $post->ID, 'homes-category', array('fields' => 'slugs')));
$permalink = str_replace( '%homes-cat%', $terms , $permalink );
return $permalink;
}
add_filter( 'post_link', 'wp_tuts_filter_post_link' , 10, 2 );
add_filter('post_type_link','wp_tuts_filter_post_link' , 10, 2 );
This will interpret,
http://domain.com/homes/2-bhk/home-1/
properly.
For more details you can also check out this blog post on Creating Custom WordPress Rewrite Rules for Pretty Permalinks

All posts and pages showing under custom post type admin menu

I've created a custom taxonomy taxonomy in a plugin for my wordpress theme. After the plugin is activated and I navigate to the admin section for the custom post type all posts and pages from the site show. Also when I try to delete these posts and pages from the custom post type admin area I get an 'invalid post type' error.
Has anyone had any experience of this happening and is there a solution?
'code'add_action ('init', 'create_post_type' );
function create_post_type() {
register_post_type( 'my_slide',
array(
'labels' => array(
'name' => __( 'Slides' ),
'singular_name' => __( 'Slide' ),
'add_new' => 'Add New',
'add_new_item' => 'Add New Slide',
'edit' => 'Edit',
'edit_item' => 'Edit Slide',
'new_item' => 'New Slide',
'view' => 'View',
'view_item' => 'View Slide',
'search_items' => 'Search Slides',
'not_found' => 'No Slides found',
'not_found_in_trash' => 'No Slides found in Trash',
'parent' => 'Parent Slide'
),
'public' => true,
'has_archive' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'map_meta_cap' => true,
'query_var' => false,
'register_meta_box_cb' => 'slide_meta_box_add',
'supports' => array('title', 'editor', 'thumbnail', 'post-formats', 'Custom Featured Image links')
)
);
}
add_action( 'init', 'create_slider_taxonomies', 0 );
function create_slider_taxonomies() {
register_taxonomy(
'slider_category',
array( 'my_slide' ),
array(
'labels' => array(
'name' => 'Slide Category',
'add_new_item' => 'Add New Slide Category',
'new_item_name' => 'New Slide Category Name'
),
'show_ui' => true,
'show_tagcloud' => false,
'show_admin_column' => true,
'hierarchical' => true
)
);
}'code'
As you stated, the problem was with the pre_get_posts() not checking if the admin area is being displayed. This can be tested for using theis_admin() condition.
function add_post_types_to_query( $query ) {
if ( (!is_admin()) && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'video' ) ); //video is a custom post type
return $query;
}
add_action( 'pre_get_posts', 'add_post_types_to_query' );
The above will stop the posts showing up in all admin areas.
There are another couple of conditions you may wish check for. With the above I had an issue with links to pages stoping working. This is solved by checking to see if the posts page is_archive(). With this in place custom post types may not show up on the home page. This can be solved by adding the is_home() condition to the function.
A final pre_get_posts function may look like.
function add_post_types_to_query( $query ) {
if ( (!is_admin()) && $query->is_main_query() )
if ( $query->is_archive() || $query-> is_home() ) {
$query->set( 'post_type', array( 'post', 'video' ) ); //video is a custom post type
}
return $query;
}
add_action( 'pre_get_posts', 'add_post_types_to_query' );
I hope this helps anyone looking at this post in the future.

Wordpress paginate_links() function, 404s and trouble

This has been a horrendous, crippling, time stealing (two week) horrible horrible problem.
It might be rewriting, my use of custom posts, but after stripping out so much (and reducing some functionality of my theme) I've reduced my problem to this:
paginate_links() is putting out a link like this:
?s=cute&post_type=image&paged=2
When I changed the variable in the browser bar to page=2 (dropping the 'd').
?s=cute&post_type=image&page=2
It works correctly.
So my question reduced to this: If I must get that function to output the "page" variable properly, how is that done?
paged and paged are both used by WordPress. So conversely, how do I get paged recognized if that is better practice?
For all I know, this is indicative of some deeper issue I have in my theme, but for the life of me I can't see how else I could be going wrong!
EDIT:
Here is my code I'm using:
if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
else
$paged = 1;
$big = 999999999; // need an unlikely integer
$stuff_to_echo = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?page=%#%',
'current' => max( 1, $paged ),
'total' => $wp_query->max_num_pages,
'type' => 'array'
) );
EDIT 2 - If above is good, here is another possible area the problem might be happening. I'll post the creation of the custom post type and the rewrite rules -
//image custom post type
add_action( 'init', 'symbiostock_image_manager_register' );
function symbiostock_image_manager_register( )
{
//creating custom post type for image
$labels = array(
'name' => 'Symbiostock Images',
'singular_name' => 'Image',
'add_new' => 'New Image',
'add_new_item' => 'Add New Image',
'edit_item' => 'Edit Image',
'new_item' => 'New Image',
'all_items' => 'All Images',
'view_item' => 'View Image',
'search_items' => 'Search Images',
'not_found' => 'No image found',
'not_found_in_trash' => 'No images found in Trash',
'parent_item_colon' => '',
'menu_name' => 'RF Images'
);
$args = array(
'labels' => $labels,
'singular_label' => __( 'Image' ),
'description' => 'Image Listings',
'menu_position' => 100,
'menu_icon' => symbiostock_IMGDIR . '/symbiostock_icon2.png',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'exclude_from_search' => false,
'supports' => array(
'title',
'editor',
'thumbnail'
),
'rewrite' => array(
'slug' => 'image',
'with_front' => false
)
);
register_post_type( 'image', $args );
register_taxonomy( 'image-type', array(
'image'
), array(
'hierarchical' => true,
'label' => 'Image Categories',
'singular_label' => 'Image Type',
'rewrite' => true,
'exclude_from_search' =>false,
'public' => true,
'slug' => 'image-type'
) );
register_taxonomy( 'image-tags', array(
'image'
), array(
'hierarchical' => false,
'rewrite' => true,
'query_var' => true,
'singular_label' => 'Image Keyword',
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'slug' => 'image-tag',
'labels' => array(
'name' => _x( 'Image Keywords', 'taxonomy general name' ),
'singular_name' => _x( 'Keywords', 'taxonomy singular name' ),
'search_items' => __( 'Search Images' ),
'all_items' => __( 'All Image Keywords' ),
'edit_item' => __( 'Edit Image Keyword' ),
'update_item' => __( 'Update Image Keyword' ),
'add_new_item' => __( 'Add New Image Keyword' ),
'new_item_name' => __( 'New Keyword Name' ),
'menu_name' => __( 'Image Keywords' ),
),
'rewrite' => array(
'slug' => 'search-images', // This controls the base slug that will display before each term
'with_front' => false,
'hierarchical' => false
),
) );
}
add_action( 'admin_init', 'symbiostock_image_directory' );
function symbiostock_image_directory( )
{
add_meta_box( 'symbiostock-image-meta', 'Symbiostock Image Info', 'symbiostock_image_manager_meta_options', 'image', 'normal', 'high' );
}
And here are the rewrite rules further down -
add_action( 'init', 'symbiostock_rewrite' );
function symbiostock_rewrite( )
{
global $wp_rewrite;
$wp_rewrite->add_permastruct('typename','typename/%year%%postname%/' , true , 1);
add_rewrite_rule('typename/([0-9]{4})/(.+)/?$','index.php?typename=$matches[2]', 'top');
$wp_rewrite->flush_rules();
}
I know this is old but I've had the same problem and solved it by changing the permalink of the page that was causing the 404.
This is because, apparently, you cannot have a page-slug with the same name as your custom post type.
All credit goes to Ryan S. for sharing the original solution: http://www.sutanaryan.com/2013/09/404-error-in-custom-post-type-pagination-wordpress/
Under your args for paginate_links() I would ensure that you have your format set properly. 'paged' is typically used for obtaining the "current page" the user is on (Reference: https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query)
'format' => '?page=%#%'
it sounds like you have it set to '?paged=%#%'
Here is a complete example
global $wp_query;
$args = array(
'format' => '?page=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
);
paginate_links($args);
It might not be the most elegant solution, but is appropriate in this case.
I simply set up a redirect to the taxonomy page in the event someone is searching this post type.
add_action( 'parse_query', 'image_search_redirect' );
function image_search_redirect( $query ) {
if ( ( is_search() && get_query_var( 'post_type' ) == 'image' ) ) {
wp_redirect(home_url("?image-tags=") . urlencode(get_query_var('s')));
exit();
}
}
This is not as much a solution as a creative work-around. There should be no reason why I get a 404 not found on the "paged=" variable. If some future person knows a better solution I'd love to know one!

Trying to get both custom post type and posts to show up in tag and category pages

I've built a custom post type, that is set to use the out-of-the-box tags and categories that posts use. However, if I click on a tag or category link, the archive only shows posts with that tag, not my custom post type. I've tried a few ways to fix it but they don't seem to be working. My code is:
// Add Resource Post Type
add_action('init', 'hallam_init');
function hallam_init() {
// set up the labels
$labels = array(
'name' => _x('Resources', 'post type general name'),
'singular_name' => _x('Resource', 'post type singular name'),
'add_new' => _x('Add New', 'resource'),
'add_new_item' => __( 'Add New Resource' ),
'edit_item' => __( 'Edit Resource' ),
'new_item' => __( 'New Resource' ),
'view_item' => __( 'View Resource' ),
'search_items' => __( 'Search Resources' ),
'not_found' => __( 'No resources found' ),
'not_found_in_trash' => __( 'No respources found in Trash' ),
'parent_item_colon' => ''
);
// set up the args
$args = array (
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array (
'slug' => 'resources'
),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments'
),
'taxonomies' => array(
'collection',
'category',
'post_tag'
),
'has_archive' => true
);
register_post_type('ht_resource', $args);
}
// Add Taxonomy
register_taxonomy('collection', 'ht_resource', array(
'hierarchical' => true,
'label' => 'Collections',
'query_var' => true,
'rewrite' => true
));
// Fix the archives
add_filter( 'pre_get_posts', 'add_to_query' );
function add_to_query( $query ) {
// if ( is_home() ) {
if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
return $query;
$supported = $query->get( 'post_type' );
if ( !$supported || $supported == 'post' )
$supported = array( 'post', 'ht_resource' );
elseif ( is_array( $supported ) )
array_push( $supported, 'ht_resource' );
$query->set( 'post_type', $supported );
return $query;
//}
}
Am I missing something obvious?
Can you plz try this by adding on your theme's functions.php? Hope it will work
function query_post_type($query) {
if(is_tag()) {
$query->set('post_type',$post_types=get_post_types('','names'));
return $query;
}
}
add_filter('pre_get_posts', 'query_post_type');
thnx

Resources