Show custom post archive when custom post not specified - wordpress

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.
The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.
What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.
Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.
I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.
I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.
The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.
This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.
Please help with suggestion on the best way I can achieve the custom taxonomy url.
Thank you all.

Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php
function custom_produce_category_link( $link, $term, $taxonomy )
{
if ( $taxonomy !== 'produce_category' )
return $link;
return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );

Related

Rewrite custom post type URL in search

I have a website in which I have a custom post type (guest authors from CoAuthors Plus). With a plugin I managed to make post of custom type "guest author" searchable by WordPress legacy search.
Now, the authors are correctly shown in search results. Although, they are linked to a wrong page, /?post_type=guest-author&p=2148, which brings to a 404.
I'd like to be able to get the URL, interprete it, and redirect to the correct page (which is in the form of /archives/author/name-surname/.
I'm trying to get it working with a rewrite URL, but I'm not able to catch the data and formulate the rewrite.
The following code changes the permalinks for guest-authors. It uses the methods from the CoAuthor plugin that output the guest authors link.
At least now you have the correct links according to the plugin's intentions.
They will be in the form:
{site_url}/author/{author_slug}
Here is the code to include in functions.php:
function adjust_permalink($permalink, $post){
$post_type = get_post_type($post);
if($post_type === 'guest-author'){
global $coauthors_plus;
$author = $coauthors_plus->get_coauthor_by('ID', $post->ID);
$permalink = get_author_posts_url( $author->ID, $author->user_nicename );
}
return $permalink;
}
add_filter('post_type_link','adjust_permalink',10,2);
Now, you should be able to create your template php file for author in your theme: author.php

wp_insert_post not saving custom taxonomy on devices

I've faced a weird problem on my wordpress website.
I am using the wp_insert_post on front end to save a custom post type and its custom taxonomy. On PC it saves perfectly the custom taxonomy, but when I try to submit on mobile or ipad, then its not saving it.
here's my code:
$post = array(
'post_title'=>$_POST['message_title'],
'post_status'=>'pending',
'post_type'=>'mondd_el',
'tax_input'=>array('me_kategoria'=>$cat_id),
'post_content'=>$_POST['my_message']
);
$postid = wp_insert_post($post);
Thanks
ARE YOU SURE THE CURRENT USER HAVE CAPABILITY ON WORKING WITH TAXONOMY ? PLEASE CHECK THAT... THIS IS WHAT YOU GET FROM WORDPRESS DOCUMENTATION
"tax_input: Equivalent to calling wp_set_post_terms() for each custom taxonomy in the array. If the current user doesn't have the capability to work with a taxonomy, then you must use wp_set_object_terms() instead."

Best method for creating a custom Wordpress post type just like a page

I have a site where I need to add a post type that has all of the exact same features as a normal PAGE in Wordpress but is a post type of "AGENT".
I thought the easiest way to do this would have been to create a normal page with a specific category that I could reference elsewhere in the code...but I know categories are not available on pages.
Is the best way to do this with a custom post type, or is there an easier method?
Thanks!
Well, we can add categories to pages.
add_action( 'init', 'wpse34528_add_page_cats' );
function wpse34528_add_page_cats()
{
register_taxonomy_for_object_type( 'category', 'page' );
}
Or you can create a Custom Taxonomy and assign it to Pages.
Or create the Custom Post Type and configure it as you wish.

Create a blog post from content in another post type

The Wordpress site I'm working on has a section for "News" (which is the regular blog/posts) which will be used for any news the company has to write about. Then I have a custom post type for Promotions, which has it's own page.
I want the client to be able to add his promotion content through the custom post type, which is going on the Promotions page, however I'd like this content to also be "cross posted" into the blog/news without forcing the client to write it up twice.
Is there a way to do this? Thanks.
Just a note: The reason I have the promotions as a custom type on it's own instead of just having them do it all from the blog is because I needed custom fields that would be unnecessary for any other kind of blog post.
Two options:
1) Use the Shortcode API
And in your cross-post you'd add the shortcode [crosspost id="POST-ID"]. Where POST-ID corresponds to the numeric ID of the other post (post type). Instead of ID, the title could be used, see the function get_page_by_title.
Create your own plugin for this. Add a sample shortcode from the Codex and use the function get_post to get the contents of the cross-post.
2) Use Advanced Custom Fields plugin
With it, adding meta boxes with custom fields is a breeze. And it has a Post Object field that's basically a Cross Post functionality.
You could do it a lot more simply by adding a filter to wp_insert_data(). For example, in your theme's functions.php file add the following:
add_filter('wp_insert_post_data', 'post_to_other', 99, 2);
That filter will then run anytime you add a new post. In the function post_to_other(), you look to see what type of post is being submitted. If it's a promotion, then insert a second copy as a News item.
function post_to_other($post_id, $post){
/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
'post_type'=> 'post',
'post_title'=> $post->post_title,
'post_name' =>$post->post_name,
'post_content'=> $post->post_content,
'post_author'=> $post->post_author,
'post_status'=> 'publish',
'tax_input'=> array('taxonomy_name'=>array('news'))
);
wp_insert_post($second_post);
}
}
I'm running out the door so I don't have time to double check the exact code but that's the basic structure of it. The tax_input bit is optional, lets you specify a category if you want. You'll probably need to tweak it a bit but that's the basics.

How to map an arbitrary URL to a function in a Wordpress plugin

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!
When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.
Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

Resources