I create WordPress page with this function:
function adv_activate_plugins(){
$post_details = array(
'post_title' => 'بازاریابی انلاین',
'post_name' =>'marketing4321',
'post_content' => '[marketing]',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
wp_insert_post( $post_details );
}
How to get_permalink of that page?
$id = wp_insert_post($post_details);
$permalink = get_permalink($id);
If you want to use it outside of your function:
function adv_activate_plugins(){
$post_details = array(
'post_title' => 'بازاریابی انلاین',
'post_name' =>'marketing4321',
'post_content' => '[marketing]',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
$id = wp_insert_post( $post_details );
return $id;
}
//the id of the new post
$new_post_id = adv_activate_plugins();
//get the permalink
$permalink = get_permalink($new_post_id);
Try with below code which would help your cause,
function get_permalink_by_post_name($post_name){
global $post;
global $wpdb;
$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$post_name."'");
return get_permalink($id);
}
echo get_permalink_by_post_name('post-name');
Related
I am having problem ordering the posts based on the title length. Here is my code:
<?php
$terms = get_terms(array(
'taxonomy' => 'vendor_category',
'slug' => 'venues',
'hide_empty' => false
));
?>
<?php
foreach ($terms as $term) {
$eventargs = array(
'post_type' => 'vendor',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'primary_category',
'meta_value' => $term->term_id,
);
$eventqry = new WP_Query($eventargs);
?>
How can i sort the posts based on the title length in ascending order.
You can save title length in post meta on save_post hook. and then you can retrieve post order by post meta value.
You can use save_post hook to save the post meta. put this code in your active theme.
//for existing vendors
add_action('admin_init', 'udpate_existing_vendor');
function udpate_existing_vendor(){
$existing_vendor_updated = get_option('existing_vendor_updated', 'no');
if( $existing_vendor_updated == 'no' ){
$vendor_args = array(
'post_type' => 'vendor',
'post_status' => 'publish',
'posts_per_page' => -1
);
$vendors = new WP_Query( $vendor_args );
if( $vendors->have_posts() ) {
while ( $vendors->have_posts() ) { $vendors->the_post();
$length = strlen( get_the_title() );
update_post_meta( get_the_ID(), 'title_length', $length );
} wp_reset_postdata();
}
update_option('existing_vendor_updated', 'yes');
}
}
// for new vendor
function save_vendor_title_length( $post_id ) {
$length = strlen( get_the_title( $post_id ) );
update_post_meta( $post_id, 'title_length', $length );
}
add_action( 'save_post_vendor', 'save_vendor_title_length');
Here your query will look like.
$terms = get_terms(array(
'taxonomy' => 'vendor_category',
'slug' => 'venues',
'hide_empty' => false
));
foreach ($terms as $term) {
$eventargs = array(
'post_type' => 'vendor',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'title_length'
);
$eventqry = new WP_Query( $eventargs );
}
I am using the code below (simplified) to display a list of the last 10 comments:
<?php
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
//'meta_key' => 'field_name',
//'meta_value' => 'field_value',
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};
?>
Question:
Well, meta_key and meta_value seem to be associated to comment_meta... But in my case, I have to display comments based on post_meta key and value.
Any sugestions?
You can try this code.
You need to add a query for posts to get array of post ides with meta key.
Then use that array into comments query argument.
//QUERY FOR POSTS WITH META KEY AND VALUE (META QUERY)
$post_args = array(
'post_type' => 'post',
'meta_key' => 'meta key',//Meta key of post
'meta_value' => 'meta value',//String or Numeric value
'meta_compare' => '=',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
Try this , then let me know the result.
My first question here at Stackoverflow and that worked perfectly.
Thank you very much, Souvik!
Below the final result (simplified):
$post_args = array(
'post_type' => 'tarefa',
'posts_per_page' => -1,
'meta_key' => 'field_name',
'meta_value' => 'field_value',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'number' => '30',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};
When I use the WP_Query, I want to filter them by titles' initial letter, Like I only want the post when the initial is between 'F-J', what should I do with it.
$query_arguments = array(
'post_type' => $atts['_type'],
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => $atts['postsPerPage'],
'ignore_sticky_posts'=> 1,
'paged' => $paged
);
$trombinoscope_query = new WP_Query($query_arguments);
You can try the mysql solution described here.
Something as :
<?php
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE (post_title like 'F%' OR post_title like 'G%' OR post_title like 'I%' OR post_title like 'F%') AND post_status='publish'");
if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild );
//Your code comes here.
endforeach; endif;
?>
I add a meta_key, it's fine for me now.
like this
function set_meta_for_employe_post() {
$post_title = get_the_title();
$post_id = get_the_ID();
if ('employe' == get_post_type()) {
if($post_title) {
add_post_meta($post_id, 'initial_letter', $post_title[0], true);
}
}
}
add_action( 'save_post', 'set_meta_for_employe_post');
and after:
$query_arguments = array(
'post_type' => $atts['_type'],
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => $atts['postsPerPage'],
'ignore_sticky_posts'=> 1,
'paged' => $paged,
'meta_key' => 'initial_letter',
'meta_value' => $letters,
);
Here is my code:
function insert_post() {
global $wpdb;
if (!isset($_POST['data'])) {
exit;
}
$data = json_decode(stripslashes_deep($_POST['data']), true);
$title = $data['post_title'];
$content = $data['post_content'];
$img = $data['post_thumbnail'];
$attach_id = $data['post_thumbnail'];
$review_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_category' => array( 34 ),
'filter' => true
);
$post_id = wp_insert_post( $review_post );
$review = get_post( $post_id );
$avatar = wp_get_attachment_image( $img, 'full' );
$common_list = array();
$common_list['post'] = $review;
$common_list['post_thumbnail'] = $avatar;
$common_list['code'] = $code;
$common_list['fb'] = $fb;
$common_list['gender'] = $gender;
echo json_encode( $common_list);
exit;
}
add_action('wp_ajax_insert_post', 'insert_post');
add_action('wp_ajax_nopriv_insert_post', 'insert_post');
Problem is that wp_insert_post() function inserts the same post 2 times. Can't find solution. What can cause this?
I found a solution, here it is:
if (!get_page_by_title($title, 'OBJECT', 'post') ){
$review_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_category' => array( 34 ),
'filter' => true
)
}
;
Just checked whether a post with same title exists or not, if yes it doesn't call wp_insert_post.
How do a select which can not click on the parent category in wp_insert_post?
Example what i want http://fiddle.jshell.net/Pp7Ey/show/
Thx for answer!
$title = $_POST['title'];
$description = $_POST['description'];
$cat = $_POST['cat'];
$data = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_category' =>array($cat)
);
wp_insert_post($data);
..
<?php wp_dropdown_categories('orderby=name&hide_empty=0&show_count=1&show_option_none=Select one&class=cat&hierarchical=1&name=cat'); ?>