Not clcickable parent category in wp_insert_post - wordpress

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

Related

Order posts by Title length in WP_QUERY

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

WordPress post title query is not working with special character

This title query doesn't work, in which the post title have a special character before the title. I check the post he title. For example : -y-
Here is the code the post single page template:
<?php
$get_s_value = $_GET['search'];
if ($get_s_value == 'true') {
$current_p_title = get_the_title();
$args = array (
'post_type' => 'trending',
'post_status' => 'publish',
"s" => $current_p_title,
);
query_posts($args);
if (have_posts()): the_post();
$sahifa_trending_count = get_post_meta(get_the_ID(), '_trending_count', 1);
$update_count = $sahifa_trending_count + 1;
$last_m_date = get_the_modified_date('Y-m-d');
$trending_c_date = date('Y-m-d');
if ($last_m_date = $trending_c_date) {
update_metadata('post', get_the_ID(), '_trending_count', $update_count);
} else {
update_metadata('post', get_the_ID(), '_trending_count', '1');
}
else:
$create_post = array (
'post_type' => 'trending',
'post_title' => get_the_title(),
'post_status' => 'publish',
'meta_input' => array (
'_trending_count' => '1',
),
);
wp_insert_post($create_post);
endif;
wp_reset_query();
}
Please try encoding the title.
$create_post = array (
'post_type' => 'trending',
'post_title' => utf8_encode(get_the_title()),
'post_status' => 'publish',
'meta_input' => array (
'_trending_count' => '1',
),
);

How to get_permalink of the wordpress page

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

In Wordpress, wp_query with special attribute

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

Want to get posts from category slug

I'm working on a project and getting same results in different category slug. Please help what I'm doing wrong here.
$act = $_POST['act'];
$args = array(
'posts_per_page' => 100,
'offset' => 0,
'category' => $act,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'product',
'taxnomy' => 'product_cat',
'post_status' => 'publish');
$myposts = get_posts( $args );
global $product;
global $wpdb;
foreach ($myposts as $key => $value) {
$id = $value->ID;
echo '<li class="product type-product status-publish has-post-thumbnail first grid with-hover add-hover open-on-mobile with-border span3 featured shipping-taxable product-type-simple product-cat-accommodation-durban product-cat-accommodation-battlefields product-cat-battlefields product-cat-comfortable-accommodation-durban product-cat-comfortable-accommodation-battlefields product-cat-durban instock">';
echo '<div class="product-wrapper">';
echo '<a class="thumb" href="'.get_permalink( $id ).'">';
$post_thumbnail_id = get_post_thumbnail_id($id);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
echo '<img src="'.$post_thumbnail_url.'" />';
echo '</a>';
echo '<h3>'.get_the_title( $id ).'</h3>';
echo '</div></li>';
}
<?php
$slug = "category-b";
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $slug
)
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
foreach ($the_query->posts as $key => $value) {
print_r($value->ID);
}
?>
After careful consideration, I found the root of the problem:
The category needs to be an ID, quoting Wordpress:
Note: The category parameter needs to be the ID of the category, and
not the category name.
Note: The category parameter can be a comma separated list of
categories, as the get_posts() function passes the 'category'
parameter directly into WP_Query as 'cat'.
Note: The category_name parameter needs to be a string, in this case,
the category name.
Taxonomy is not well written, is "taxonomy" and not "taxnomy". Also, you can remove it from there as it's not filtering anything.

Resources