Is there a way to change the default template for all posts in wordpress? - wordpress

I am creating posts in wordpress programmatically using php. Is there a way to change the post attributes template either programmatically or using the admin panel for all posts? I would like the default to be Product details as you see in the picture below.
Any help appreciated. Thanks.

after creating post programmatically assign page template.
update_post_meta( $post_id, '_wp_page_template', 'page-template.php' );

Related

WordPress ACF custom taxonomy field display

I'm fairly new to WordPress and using the ACF plugin for the first time. I've created a custom post type with custom fields, two of which are of type taxonomy that are tied to custom taxonomies I set up specifically for this post type. That all works great, but I'd like for the custom taxonomies to only show on the post form and not in the right sidebar since that's redundant and confusing for the editors:
Is there anyway to hide them in the sidebar? I already checked categories and tags under the "hide on screen" option for the custom field group, but that didn't seem to make a difference.
Thank you!
add_action( 'admin_menu' , 'wpdocs_remove_post_custom_fields' );
function wpdocs_remove_post_custom_fields() {
remove_meta_box( 'META_BOX_ID' , 'CUSTOM_POST_TYPE' , 'normal' );
}
add in your active themes file -> functions.php
Following parameters:
META_BOX_ID: https://prnt.sc/3TLMZCHCak17
CUSTOM_POST_TYPE: add you custom post type

Show custom post archive when custom post not specified

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

Can I add category to my custom taxonomy through some function in wordpress?

I use my custom taxonomy on Wordpress project and I need to create new category through code.
Is that even possible? I can`t google some solution for my problem.
Use wp_insert_term
wp_insert_term( 'category_name', 'your_custom_taxonomy' );

Can we replicate ACF functionality in our theme?

Hi i want to add advance custom fields in my wordpress theme. I don't need editor functionality but to provide the users the facility to enter the values to view in the frontend.
basically i want to add the custom fields in my theme just like ACF but i don't want to use the plugin. Is there anything anyone can help me out with this please do.
I'm using ACF plugin right now to add custom fields in my theme.
For example I'm getting the designation from user in the admin panel and our team members custom post type. and showing it on the home page by using this code.
<p><span class="fa fa-user-circle"></span> Designation</strong>:<?php echo the_field( 'designation' ); ?> </p>
I don't want to use ACF plugin to perform this task. I know about the wordpress custom field. The problem with wordpress custom field is I've to select the key every time when I create a post. Here's a sample of what I'm trying to do. I want to add the this in my add new post.
Right now I've to select the key value whenever a new post is created. I want something similar to the image attached in my add new post. Thanks.
You can use native WordPress function(s) to do the same thing. Of course, you lose the extensive ACF features and slick ACF admin panels.
Instead of using ACF's get_field(), you can use get_post_meta(get_the_ID(), 'field', true);
List all items in the meta field 'appartments' like so:
$meta_name = 'appartments';
$fields = get_post_meta( $post->ID, $meta_name, true );
foreach ( $fields as $fieldValue )
{
echo $fieldValue . ' ';
}

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.

Resources