Flutter - Retrieve Wordpress content that is not Post [duplicate] - wordpress

I'm trying to get a list of taxonomies using the WordPress REST API. Hitting /wp-json/wp/v2/taxonomies/post_tag works fine, but I also have a custom taxonomy called location and accessing /wp-json/wp/v2/taxonomies/location returns a 403 rest_forbidden error.
I can't figure out under what circumstances taxonomy REST access would be forbidden in this way. Any ideas?

You need to set show_in_rest to true when registering your taxonomy.
https://codex.wordpress.org/Function_Reference/register_taxonomy
If your custom taxonomy was created by a plugin and you need to alter it's behaviour try this post :
http://scottbolinger.com/custom-taxonomies-in-the-wp-api/
In short, you can add the code below to your functions file to enable show_in_rest for all custom taxonomies.
function prefix_add_taxonimies_to_api() {
$args = array(
'public' => true,
'_builtin' => false
);
$taxonomies = get_taxonomies($args, 'objects');
foreach($taxonomies as $taxonomy) {
$taxonomy->show_in_rest = true;
}
}
add_action('init', 'prefix_add_taxonimies_to_api', 30);
I hope this helps you.

Related

How to have a page and custom-post-type on same hierarchy

I am searching for a way to have "jobs" and a page with an "application form" in the same url-hierarchy in WordPress.
The jobs (CPT):
/jobs/engineer/
/jobs/ceo/
The application-form page (Page):
/jobs/application-form/
In my understanding its not possible to mix "posts" and "pages" in WordPress.
Maybe I have to help myself by adding "/detail/" or something else to the jobs.
Thanks for help!
In my understanding its not possible to mix "posts" and "pages" in WordPress.
That's right, because post aren't hierarchical, but pages are.
When you register your custom post type using register_post_type() you can set it as hierarchical using the arguments hierarchical. The default value being false.
If you set 'hierarchical' => true your custom post type will act like a PAGE, with hierarchical capabilities. If you set 'hierarchical' => false your custom POST type will act like a post, without hierarchical capabilities.
Once this is done, after setting a custom post type as jobs with 'hierarchical' => true and creating a parent page called details and a child page called application-form will get you /jobs/details/application-form/.
add_action( 'init', function() {
$args = [
// ...
'hierarchical' => true,
// ...
];
register_post_type( '_your_custom_post_type_slug_', $args );
} );

Plugin single.php not showing

I'm creating a plugin for Wordpress that creates a new post type for news and events (yes, yet another one ;-) ). The post-type is registered under the name gg_nae.
The post type works as expected; the post type can be saved and edited.
Now I want to create a custom template file. I called it single-gg_nae.php and I saved it in the same folder as the plugin-code.
If I understand the explanation on https://developer.wordpress.org/plugins/post-types/working-with-custom-post-types/ correctly that should be enough to render the post type in the custom template, but it won't. It renders the post with the single.php file from the template.
If I move the custom template to the theme-folder however, Wordpress uses the template as intended; it renders the post in the single-gg_nae.php template.
What am I doing wrong here? Should single-gg_nae.php be placed in a specific folder in the plugin-map?
edit: I already saved the permalink-structure again, but that didn't help.
only for the missing in this three below parameter u are getting error
'rewrite' => array( 'slug' => 'slider' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
try it in argument list with rest of parameters
I was thinking all wrong when approaching this issue.
I have created custom post types before and always included them in the functions.php of my Wordpress theme, therefor creating templates (archive-PostTypeName.php and single-PostTypeName.php) to show them. I assumed that was also needed when developing a CPT-plugin.
In case of a plugin, however, you have to alter the behavior of the single.php template from within the plugin using filters. I have added the following (simplified) code to my plugin:
/**
* Alter the_content if Posttype is custom post type
*
* #uses is_single()
*/
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
if ( is_single() AND get_post_type() === 'PostTypeName') {
// Do stuff here
$content = 'This is My Custom Post Type!';
}
// Returns the content.
return $content;
}
When opening posts of the type 'PostTypeName' the content will say 'This is My Custom Post Type!', otherwise it'll show the content of the post of page.
More information about filters (and actions) can be in the Wordpress Codex

Adding a link in post-type list

I want to add a custom link in the posts of my post-type with the filter post_row_actions but there's no additional link.
I use this code :
function custom_duplicate_link( $actions, $post ) {
$actions['duplicate'] = 'Duplicate';
return $actions;
}
add_filter( 'post_row_actions', 'custom_duplicate_link', 10, 2 );
Have I missed anything? Or does anyone know why it isn't working?
Check your custom post type:
'hierarchical' => TRUE,
The filter 'post_row_actions' only work with post types not hierarchical.
The filter 'page_row_actions' only work with post types hierarchical.

Wordpress custom post type permalink parent/child but different types

In wordpress I created 2 custom post types: company and offer
offers are linked to companies with a customfield called companyname.
So if an offer called 'My offer' is linked to Company 'ACME', both posttype contain a customfield called 'companyname' with value of ACME.So far no problem.
I registered both custom posttypes with a default slug 'company' and 'offer'
The shop permalink url is now: www.mywebsite.nl/company/acme/
The offer permalink url is now: www.mywebsite.nl/offer/my-offer/
What I want is the offer permalink is changed to www.mywebsite.nl/company/acme/my-offer/
I searched a lot through filters/action/hooks (and stackoverflow), but I cannot find a solution. I even tried to make the post-types hierarchical, but in an 'offer' posttype I cannot make a 'company' posttype the parent.
How can get the url structure I need?
There's two things you need to solve.
First, you need the url for the offers to be changed to your new form. This is done by specifying a slug that you can modify for each of the offers.
When registering the post type, add a variable to the slug:
// add modified
register_post_type('company_offer_type',array(
...
'rewrite' => array(
'slug' => 'company/%company%'
),
));
// filter to rewrite slug
add_filter( 'post_type_link', 'postTypeLink', 10, 2);
function postTypeLink($link, $post)
{
if (get_post_type($post) === 'company_offer_type') {
$company = // get company from $post
$link = str_replace('%company%', $company, $link);
}
return $link;
}
Second, you want wordpress to redirect incoming url requests to this post. Do this by adding a rewrite rule on the init action:
// add rewrite rule for custom type
add_rewrite_rule(
'company/(.*)/(.*)',
'index.php?post_type=company_offer_type&name=$matches[2]',
'top'
);
Here are two articles with more info:
http://code.tutsplus.com/articles/the-rewrite-api-the-basics--wp-25474
http://code.tutsplus.com/articles/the-rewrite-api-post-types-taxonomies--wp-25488

Using Dates in Custom post_type Permalinks in Wordpress 3.0

I'm adding a custom post_type to Wordpress, and would like the permalink structure to look like this:
/%post_type%/%year%/%monthnum%/%postname%/
I can't figure out how to add the date tags. Using this code, gives me /my_type/example-post-slug/:
register_post_type( 'customtype', array(
...other options...
'rewrite' => array('slug' => 'my_type'),
));
How do I include the dates?
You can achieve this with the plugin Custom Post Type Permalinks. Just install the plugin and change the permalink format in the settings.
You need to add WordPress structure tags to your rewrite attribute like so:
register_post_type('customtype',array(
....
'rewrite' => array('slug' => 'customtype/%year%/%monthnum%','with_front' => false)
));
Then add a post_type_link filter to rewrite the structure tags in your URLs for the custom post, so that the tags work:
function custompost_post_type_link($url, $post) {
if ( 'customtype' == get_post_type($post) ) {
$url = str_replace( "%year%", get_the_date('Y'), $url );
$url = str_replace( "%monthnum%", get_the_date('m'), $url );
}
return $url;
}
add_filter('post_type_link', 'custompost_post_type_link', 10, 2);
You can refer to this article for copypasta code (encapsulated in a class though) on creating custom posts like this. The article has a few extra bits of explanation and a few pieces of additional functionality too: https://blog.terresquall.com/2021/03/making-date-based-permalinks-for-custom-posts-in-wordpress/
EDIT: By the way, you'll also need to flush your permalinks after you're done for this to work.
I have found a partial solution which allows for the permalink to be recognized and preserved upon loading the page in the address bar, but not updated in the edit screen or other links to the post on the site.
Add the following to functions.php or a site specific plugin, replacing example-post-type with the identifier of your post type.
function example_rewrite() {
add_rewrite_rule('^example-post-type/([0-9]{4})/([0-9]{1,2})/([^/]*)/?','index.php?post_type=example-post-type&year=$matches[1]&monthnum=$matches[2]&name=$matches[3]','top');
}
add_action('init', 'example_rewrite');
This uses the Rewrite API documented here
To find more tips on understanding the process see here.
One thing to bear in mind is no matter how you do this, it is impossible for two posts to have the same slug, even if they have different dates. This is because if the permalink scheme is ever changed, they could clash and cause errors.
Use this it's working 100% :
'rewrite' => array('slug'=>date('Y').'/'.date('m').'/custom_post_type_slug','with_front'=>true)

Resources