I am developing a Wordpress plugin. I have some links for showing product Brands name and Product categories listing, in the url: http://testsite.com/product-categories and http://testsite.com/brands like that. So I want this link will be auto generated when plugin is activated and place in the default menu in Wordpress. How can I do that. I tried this code but its not working.
register_activation_hook( __FILE__, array( $this, 'myplugin_activate' ) );
public function myplugin_activate() {
$product_categories = array(
'post_content' => '',
'post_name' => 'product-categories',
'post_title' => 'Product Categories',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
$brands = array(
'post_content' => '',
'post_name' => 'brands',
'post_title' => 'Brands',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
wp_insert_post( $product_categories );
wp_insert_post( $brands );
}
Can anyone tell me how to do that? One thing I can not do that as page post type.
Searching for wp_insert_post and nav_menu_item, I found Programmatically add a Navigation menu and menu items where wp_create_nav_menu is suggested.
Should be a matter of using the functions wp_get_nav_menu_object, wp_create_nav_menu, wp_update_nav_menu_item and get_nav_menu_locations.
Related
I have made an action that allows a page to be made if the switch in the setting is checked. After it is confirmed it's checked it does create the page, but without the template added. How do I get my template to work?
add_action( 'admin_init', 'cart_page' );
function cart_page() {
//Add cart page to website
if ( get_option( 'cart' ) === "checked" AND get_option('cart_exist') === false) {
// IF CART HAS BEEN CHECKED
$new_page_id = wp_insert_post( array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
// Assign page template
'page_template' => plugins_url('page_templates/cart_template.php', __FILE__ )
) );
wp_insert_post($new_page_id);
update_option( 'cart_exist', true );
}
else if (get_option('cart') === "" AND get_option('cart_exist') === true) {
// IF CUSTOMER DOES NOT WANT CART
update_option( 'cart_exist', false );
}
}
This is what my template page looks like in the plugin.
get_header();
echo do_shortcode( '[cart_portal]' );
get_footer();
?>
In my case, I have figured out a solution for the simple problem. If I just need to add a shortcode I can add it to 'post_content'. I would still like to know how to add template. But you can use a file to import the entire layout if you'd like still. But it cannot be used with visual composers.
See example below...
$new_page_id = array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '[cart_portal]',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
);
wp_insert_post($new_page_id);
i used wp_insert_post to add posts to custom post type i had created but i need to insert the posts in category , i used post_category=> array($category_id) but it didn't work .. here is the code
$my_post = array(
'post_type' => 'articles',
'post_title' => "test",
'post_content' => "blabla",
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(38),
);
wp_insert_post( $my_post );
You can set it after the post creation with:
$catid = 38;
wp_set_post_categories($my_post,$catid,false);
If you're not sure what's the category ID you can use the category slug:
$catid = get_category_by_slug( 'xxx' );
The category taxonomy, of course, should already exist in that Custom Post Type.
You can set it in the CPT definition with:
'taxonomies' => array( 'category' ),
I have Most viewed posts(in Customer Category), in home page I have added this widget and it works perfect, in detail page also it works perfect but in category page the same widget behaves strangely
$r = new WP_Query( array( 'tax_query' =>
array(
'relation' => 'OR',
array(
'taxonomy' => 'custcategory',
'field' => 'term_id',
'terms' => array(10)),
),
'category__in'=>array(10),
'post_type'=> $post_type ,
'posts_per_page' => $number,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC' ) );
if ($r->have_posts()) :
// Enters this block in home page and detail page
while ( $r->have_posts() ) : $r->the_post();
else:
// Enters this block in category page
Anyone know why this strange behavior is?
Maybe you have some filter that is affecting categories page, add this to your query args:
'suppress_filters' => true
You can also try adding wp_reset_query() after your main query, but this should not be necessary because you are declaring a new WP_Query.
I just realize that your query args are wrong. You have set a relation, but you only have one taxonomy array. Also you don't need the 'category__in'=>array(10) argument, it is only for the default taxonomy "category" and you want only posts from your custom taxonomy "custcategory", isn't it?
Your query should be:
$r = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'custcategory',
'field' => 'term_id',
'terms' => array(10)
),
),
'post_type'=> $post_type ,
'posts_per_page' => $number,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
i have created a plugin that creates a page on activation.
now i cant find a way to update the content of the page programitically
i have been searching the internet for a while could not find an answer
here is how i create a new page using my plugin
function some_function()
{
$post_details = array(
'post_title' => 'Page title',
'post_content' => 'Content of your page',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
wp_insert_post( $post_details );
}
register_activation_hook(__FILE__, 'some_function');
I use the code like below in a submit page.
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_excerpt' => $excerpt,
'post_category' => array($_POST['cat']),
'tags_input' => array($tags),
'post_status' => 'publish',
'post_type' => 'custom_post_type'
);
$pid = wp_insert_post($new_post);
The $_POST['cat'] fetches the correct `category id. My custom taxonomy is named directory.
I am able to view in the saved post in the backend but the category checkbox is not checked there and in the front end i am displaying the post under these custom category, but it doesn't appear.
Is there a way to save the custom category of a custom taxonomy properly.
To assign a taxonomy to a post:
$cat_ids = array( $new_cat_id );
$cat_ids = array_map('intval', $cat_ids);
wp_set_object_terms( $post_id, $cat_ids, 'my_tax_name' );