how to add new pages using WP core function - wordpress

how can I add a page using core functions of wordpress.I can find function for Post etc as well but couldn't find any for Pages.Any one who can help me
Thanks

You use the same function to add posts or pages, just add the post_type parameter like this :
$args = array( 'post_type' => 'page',
'post_content' => 'You content...',
'post_parent' => $parent_id; // ID of the page this one should be a child of
... // etc.
...
'post_title' => 'Title for your page');
wp_insert_post ($args);

Related

How to filter custom fields for custom post type in wordpress rest api?

I use wordpress standard with the plugins "Advanced custom fields" and "custom_post_type ui". I created a post_type called deals and added some custom fields with it.
What I need to do now, is filter the results when accessing the rest api like this:
http://localhost:8000/wp-json/wp/v2/deals
Actually I only need the acf part of it. I dont care about the rest.
[{"id":29,"date":"2019-04-12T12:34:14","date_gmt":"2019-04-
12T12:34:14","guid":{"rendered":"http:\/\/localhost:8000\/?
post_type=deals&p=29"},"modified":"2019-04-
12T12:34:14","modified_gmt":"2019-04-12T12:34:14",
"slug":"test-title","status":"publish","type":"deals",
"link":"http:\/\/localhost:8000\/deal s\/test- title\/","template":"",
"meta":[],"tax-deals":[],"acf":{"title":"Title for Deals
Post","description":"","image":false,"date_start":"01.01.1970",
"date_end":"01.01.1970","category":"Kleidung"},"_links":{"self":
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/deals\/29"}],
"collection":[{"href":"http:\/\/localhost:8000\/wp-
json\/wp\/v2\/deals"}],"about":[{"href":"http:\/\/localhost:8000\/wp-
json\/wp\/v2\/types\/deals"}],"wp:attachment":
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?
parent=29"}],"wp:term":[{"taxonomy":"tax_deals","embeddable":true,
"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tax-deals?
post=29"}],"curies":
[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},
I have already tried using
http://localhost:8000/wp-json/wp/v2/deals?search=id
to get the id or something, but response is empty.
Also this didnt work:
http://localhost:8000/wp-json/wp/v2/deals?id=28
Again empty response.
To summarize: I need to filter my custom post type on my custom fields by the "acf" attribute shown in my response json. How does it work?
EDIT: I already installed "WP REST Filter" but still dont know how to do it.
I suggest you to create a new API where you can customize the output. Take advantage of wordpress function register_rest_route() using this you can create an API from CPT and ACF in one ajax url. And you do not need to install anything.
Check how I get my instructor CPT and mycheckbox ACF.
// your ajaxurl will be: http://localhost/yoursite/wp-json/custom/v2/instructor/
add_action( 'rest_api_init', function () {
register_rest_route( 'custom/v2', '/instructor', array(
'methods' => 'GET',
'callback' => 'instructor_json_query',
));
});
// the callback function
function instructor_json_query(){
// args to get the instructor
$args = array(
'post_type' => 'instructor',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'mycheckbox', // your acf key
'compare' => '=',
'value' => '1' // your acf value
)
)
);
$posts = get_posts($args);
// check if $post is empty
if ( empty( $posts ) ) {
return null;
}
// Store data inside $ins_data
$ins_data = array();
$i = 0;
foreach ( $posts as $post ) {
$ins_data[] = array( // you can ad anything here and as many as you want
'id' => $posts[$i]->ID,
'slug' => $posts[$i]->post_name,
'name' => $posts[$i]->post_title,
'imgurl' => get_the_post_thumbnail_url( $posts[$i]->ID, 'medium' ),
);
$i++;
}
// Returned Data
return $ins_data;
}
Then, you can use the link: http://localhost/yoursite/wp-json/custom/v2/instructor/ in your ajax url.

WordPress: Taxonomy archives based on Custom Post Type

I've generated three different custom post types (e.g. books, movies, games).
And I've a custom taxonomy for all of them (e.g. genre).
What I need are archives for the taxanomy based on the post types.
For example: "books-genre", "movies-genre"...
Is there any solution to do that? Now I've only the taxonomy archive for "genre".
The way I like to approach custom post archives is to create a custom archive template with WP_Query sections where I need them. You'd create the blank file in the root of your theme at archive-cptnamehere.php.
You might have some template partials to add in but the core of the page looks like this:
<?php
// 1- Get ID of the page's path by URL (reliable)
$pagePath = $_SERVER['REQUEST_URI'];
$pageObject = get_page_by_path($pagePath);
$pagePathID = $pageObject->ID;
// 2- Print page title
$teamPageTitle = get_the_title($pagePathID);
echo $teamPageTitle;
// 3 - Do a query that gets the data you need
// Args: -1 shows all locations, orders locations alphabetically by title, orders locations a-z, only query against team items
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'team',
'meta_query' => array(
array(
'key' => 'team_page_categorization',
'value' => $team_page_categorization_options_array_item,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
// 4- Setup query while loop
if($the_query->have_posts()) {
while($the_query->have_posts()) {
$the_query->the_post();
// 5- Do what you need to inside the loop
// 6- Close it all up, don't forget to reset_postdata so you can do additional queries if necessary!
}
wp_reset_postdata();
}
?>

How to list, custom page templates usage?

I created a custom page template. And i assign a page its name called like 'Authors', (but it can be change for my customer).
/**
* Template Name: Authors
* Authors page template.
* #package News V2.
* #version v12096.01-stable
*/
The point,
Could we list which page use my custom page template ? If we how ? Thanks.
function getAresAuthorsPage(){
global $post;
$args =array(
'post_type' => 'page',
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_wp_page_template',
'meta_value' => 'template-authors.php'
);
$Crumbpage = get_posts( $args );
return 'ALL AUTHORS';
}
we need to use, meta_query for listing that page template usage ;)
Have a nice one.
Note. Sorry about my really bad English.

Add a page to wordpress

I'm currently in the process of building my own WordPress plugin.So i need to create a new page(or post) that will be automatically added to word press when the plugin is activated.And this will be removed when plugin is deactivated.Content in the page is what content i am typing in the plugin.
HOW CAN I DO THAT?
you can use wp_insert_post function to create page or post check this http://codex.wordpress.org/Function_Reference/wp_insert_post
ex.
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post);
you can use $post_id withing add_post_meta or update_post_meta or use post_meta varible to install and uninstall the page.
Something like this? There's some code referenced in the linked post for dealing with page creation. There's also the codex.

How to prevent wp_insert_post() to set Uncategorized category?

I'm using Wordpress 3.5 and it seems that wp_insert_post() cannot set categories anymore, the documentation syas :
post_category no longer exists, try wp_set_post_terms() for setting a
post's categories
The problem is that wp_set_post_terms() or wp_set_object_terms() require the postID, which is returned by wp_insert_post(). While this is fine to set category terms to the post inserted by wp_insert_post(), the problem is that every time I call wp_insert_post() I get the Uncategorized category in my post, in addition to the category terms I set after calling wp_insert_post(). How can I prevent the Uncategorized to be always there?
I don't know where have you found that wp_insert_post() can't set categories anymore but from the WordPress Doc you can do it like
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39) // id's of categories
);
// Insert the post into the database
wp_insert_post( $my_post );
Bellow is an working example of mine that I'm using in one my site to add new post dynamically by an admin with a category name of location with two meta fields, input taken from the user (I've filtered user inputs but omitted here)
$category='location'; // category name for the post
$cat_ID = get_cat_ID( $category ); // need the id of 'location' category
//If it doesn't exist create new 'location' category
if($cat_ID == 0) {
$cat_name = array('cat_name' => $category);
wp_insert_category($cat_name); // add new category
}
//Get ID of category again incase a new one has been created
$new_cat_ID = get_cat_ID($category);
$my_post = array(
'post_title' => $_POST['location_name'],
'post_content' => $_POST['location_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array($new_cat_ID)
);
// Insert a new post
$newpost_id=wp_insert_post($my_post);
// if post has been inserted then add post meta
if($newpost_id!=0)
{
// I've checked whether the email and phone fields are empty or not
// add both meta
add_post_meta($newpost_id, 'email', $_POST['email']);
add_post_meta($newpost_id, 'phone', $_POST['phone']);
}
Also remember, every time you add a new post without a category, WordPress sets the default category for that post and it's uncategorized if you did not change it from the admin panel, you can change the default category from uncategorized to anything you want.
Update:
Since the post_category is no more exists so you can replace
'post_category' => array($new_cat_ID)
with following
'tax_input' => array( 'category' => $new_cat_ID )
in the example given above. You can also use
$newpost_id=wp_insert_post($my_post);
wp_set_post_terms( $newpost_id, array($new_cat_ID), 'category' );
Remember that, in this example, the $new_cat_ID has been found using following line of code
$new_cat_ID = get_cat_ID($category);
but it's also possible to get the category id using following code
$category_name='location';
$term=get_term_by('name', $category_name, 'category');
$cat_ID = $term->term_id;
Read more about get_term_by function.

Resources