function wporg_add_custom_box(){
$screens = ['product', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Select a Authors Name ', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action('edit_form_after_title', 'wporg_add_custom_box');
function wporg_custom_box_html($post){
$value = get_post_meta($post->ID, '_getwriter_id', true);
?>
<select name="book_writer_id" id="book_writer_id" class="postbox">
<?php
$type = 'book_authors'; // your post type
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); $id = get_the_ID();?>
<option value="<?=$id; ?>" <?php selected($value, $id); ?>><?=the_title(); ?></option>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</select>
<?php }
function wporg_save_postdata($post_id)
{
if (array_key_exists('book_writer_id', $_POST)) {
update_post_meta(
$post_id,
'_getwriter_id',
$_POST['book_writer_id']
);
}
}
add_action('save_post', 'wporg_save_postdata');
I did not used there save_postdate function that why, it not saving value and updating data
function wporg_add_custom_box(){
$screens = ['product', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Select a Authors Name ', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action('edit_form_after_title', 'wporg_add_custom_box');
function wporg_custom_box_html($post)
{
$value = get_post_meta($post->ID, '_getwriter_id', true);
?>
<select name="book_writer_id" id="book_writer_id" class="postbox">
<?php
$type = 'book_authors'; // your post type
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); $id = get_the_ID();?>
<option value="<?=$id; ?>" <?php selected($value, $id); ?>><?=the_title(); ?></option>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</select>
<?php
}
function wporg_save_postdata($post_id)
{
if (array_key_exists('book_writer_id', $_POST)) {
update_post_meta(
$post_id,
'_getwriter_id',
$_POST['book_writer_id']
);
}
}
add_action('save_post', 'wporg_save_postdata');
Related
I need a help regarding archive-product hook.
i am writing this code in functions.php file then the products will shown before a header(screenshot is given),
how i can display products in properly??
please help me!
function so_27975262_product_query( $q, $details = [] ){
$details = warehouse_detail();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post();
do_action('woocommerce_shop_loop');
$prod_img = wp_get_attachment_url(get_post_thumbnail_id(get_the_id()));
$product = wc_get_product(get_the_id());
$price = $product->get_price();
$werehouseDetails = unserialize(get_post_meta(get_the_ID(), 'warehouse_id', true));
$billing_state = get_user_meta(get_current_user_id(), 'billing_state', true);
$warehouse_qty = [];
foreach ($werehouseDetails as $werehouseDetail) {
if (in_array($werehouseDetail, $details)) {
$warehouse_qty = get_post_meta(get_the_ID(), 'warehouse_qty_' . $werehouseDetail, true);
if ($warehouse_qty > 0) { // Shifa End 20-10-22
$products = wc_get_template_part('content', 'product');
}
}
}
endwhile; wp_reset_query();
endif;
$q->set('query',$products);
}
add_action( 'woocommerce_product_query', 'so_27975262_product_query' );
Hi Hope You All doing well, I implemented a search for posts that are connected to categories child and then the parent of that child everything works perfect in search but I want to bring the search post under its own category and not to repeat that category title again and again for the post title here is my code script and check screenshot here what I mean to say
screenshot before search https://prnt.sc/10kzkvp
screenshot after search https://prnt.sc/10kzmxt As you can see here I want to list posts inside its own category and not repeat the category title for its post, let me know if need more explanation
<?php
$args = array(
'post_type' => 'post',
'extend_where' => "(post_title like '%$s_word%')",
'posts_per_page' => -1,
'orderby'=> 'title',
'order' => 'ASC'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$icon_flag = false;
$child_c = "";
$parent_c = "";
while ( $the_query->have_posts() ) {
$the_query->the_post();
$split_title = explode(',', get_the_title(), 2);
$id = get_the_ID();
$current_cate = get_the_terms($id,'category');
$parent = $current_cate[0]->parent;
//load object for parent category
$parent_name = get_the_category_by_ID($parent);
if($parent_name != $parent_c && $parent != 0)
{
$parent_c = $parent_name;
?>
<h1 class="grand-p-title p-parent"><?php echo $parent_c; ?></h1>
<?php
}
if($current_cate[0]->name != $child_c )
{
$child_c = $current_cate[0]->name;
?>
<!-- <h1 class="grand-p-title p-child"><?php //echo $current_cate[0]->name; ?></h1> -->
<?php
}
?>
<p class="post-letters"><span class="first-w"><?php echo $split_title[0].", ";?></span><?php echo $split_title[1]; ?></p>
<?php
}
}else
{
?>
<p class="post-letters"><a>No posts founds</a></p>
<?php
}
wp_reset_query();
?>
I think you are doing wrong. you need to loop of categories first and then based on the category you have to get posts. for fetch all categories you can use WP get_terms()
<?php
$terms = get_terms( array (
'taxonomy' => 'category',
'hide_empty' => false
) );
// run a query for each term
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'post',
'cat' => $term->term_id,
'extend_where' => "(post_title like '%$s_word%')",
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
);
// run the query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) {
$icon_flag = false;
$child_c = "";
$parent_c = "";
while ( $the_query->have_posts() ) { $the_query->the_post();
$split_title = explode(',', get_the_title(), 2);
$id = get_the_ID();
$current_cate = get_the_terms($id,'category');
$parent = $current_cate[0]->parent;
//load object for parent category
$parent_name = get_the_category_by_ID($parent);
if($parent_name != $parent_c && $parent != 0) {
$parent_c = $parent_name; ?>
<h1 class="grand-p-title p-parent"><?php echo $parent_c; ?></h1>
<?php }
if($current_cate[0]->name != $child_c ) {
$child_c = $current_cate[0]->name; ?>
<!-- <h1 class="grand-p-title p-child"><?php //echo $current_cate[0]->name; ?></h1> -->
<?php } ?>
<p class="post-letters"><span class="first-w"><?php echo $split_title[0].", ";?></span><?php echo $split_title[1]; ?></p>
<?php }
}else { ?>
<p class="post-letters"><a>No posts founds</a></p>
<?php } wp_reset_query();
}
hi all i need create to metabox in page for the footer and header posttypes i created, in this code i tried for this but i cant save selected item and retrive id selected item in the frontend.
function wpdocs_register_meta_boxes() {
add_meta_box( 'meta-box-id', __( 'header', 'textdomain' ),'wpdocs_my_display_callback', 'page', 'normal' , 'high');
}
add_action( 'add_meta_boxes', 'wpdocs_register_meta_boxes' );
function wpdocs_my_display_callback() {
?>
<select name="count" id="selectId" >
<?php
$posts = new WP_Query(array('posts_per_page' => -1, 'post_type' => 'header'));
while($posts->have_posts()) : $posts->the_post(); ?>
<option id="selection" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile;
die()
?>
</select>
<?php
}
function wpdocs_save_meta_box( $post_id ) {
if (isset($_POST['selectId'])) {
update_post_meta( $post_id, 'selectId', $_POST['selectId']);
}
}
add_action( 'save_post', 'wpdocs_save_meta_box' );
you can directly use by postId like below
echo esc_attr( get_post_meta( get_the_ID(), 'selectId', true ) );
Change your wpdocs_my_display_callback function like below.
function wpdocs_my_display_callback( $post ) {
$selectId = get_post_meta( $post->ID, 'selectId', true );
?>
<select name="selectId" id="selectId" >
<?php
$posts = new WP_Query(array('posts_per_page' => -1, 'post_type' => 'header'));
while($posts->have_posts()) : $posts->the_post(); ?>
<option id="selection" value="<?php echo get_the_ID(); ?>" <?php selected( $selectId, get_the_ID() ); ?>><?php echo get_the_title(); ?></option>
<?php endwhile;
wp_reset_query();
?>
</select>
<?php
}
thanks for answer and that work, But in need two metabox one for header select and one for select footer. I use this code for but not work, when comment and disable section this code for header, footer is work and save. when comment footer section code, header is work and save options seleceted!!!
function header_register_meta_boxes() {
add_meta_box( 'meta-box-header', __( 'header', 'poedit' ),'header_my_display_callback', array('page','post'), 'side' , 'high');
}
add_action( 'add_meta_boxes', 'header_register_meta_boxes' );
function header_my_display_callback()
{
global $post;
$header_value = get_post_meta($post->ID, 'selected_header', true);
?>
<select name="selected_header" id="selected_header">
<?php
$posts = new WP_Query(array('posts_per_page' => -1,'post_status' => 'publish', 'post_type' => 'header'));
echo'<option value="0">select header</option>';
while ($posts->have_posts()) : $posts->the_post(); ?>
<option <?php selected($header_value, get_the_ID()); ?> id="selection_header" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile;
wp_reset_query();
?>
</select>
<?php
}
function header_save_meta_box( $post_id ) {
if (isset($_POST['selected_header'])) {
update_post_meta( $post_id, 'selected_header', $_POST['selected_header']);
}
}
add_action( 'save_post', 'header_save_meta_box' );
function footer_register_meta_boxes() {
add_meta_box( 'meta-box-footer', __( 'footer', 'poedit' ),'footer_my_display_callback', array('page','post'), 'side' , 'high');
}
add_action( 'add_meta_boxes', 'footer_register_meta_boxes' );
function footer_my_display_callback()
{
global $post;
$footer_value = get_post_meta($post->ID, 'selected_footer', true);
?>
<select name="selected_footer" id="selected_footer">
<?php
$posts = new WP_Query(array('posts_per_page' => -1,'post_status' => 'publish', 'post_type' => 'footer'));
echo'<option value="0">select footer</option>';
while ($posts->have_posts()) : $posts->the_post(); ?>
<option <?php selected($footer_value, get_the_ID()); ?> id="selection_footer" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile;
wp_reset_query();
?>
</select>
<?php
}
function footer_save_meta_box( $post_id ) {
if (isset($_POST['selected_footer'])) {
update_post_meta( $post_id, 'selected_footer', $_POST['selected_footer']);
}
}
add_action( 'save_post', 'footer_save_meta_box' );
I have problem with displaying products from category with ajax.
I've rendered all categories and all products on page and my categories are links and I prevented default action on links and I want to achieve this. When you click on the link of the category, that triggers ajax and show bellow products from that category, everything happens on same page without reload. But all I got is "no post found" when i click on category link
function ajax_filter_get_posts( $taxonomy ) {
// Verify nonce
if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
die('Permission denied');
$taxonomy = $_POST['taxonomy'];
// WP Query
$args = array(
'tag' => $taxonomy,
'post_type' => 'product',
'posts_per_page' => 10
);
if( !$taxonomy ) {
unset( $args['tag'] );
}
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$output = '<h2>'. get_the_title().'</h2>';
$output .= get_the_excerpt();
$result = 'success';
endwhile; else:
$output = '<h2>No posts found</h2>';
$result = 'fail';
endif;
$response = json_encode($output);
echo $response;
die();
}
add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
<article>
<?php
/**
* Template name: AJAX Post Filter by Taxonomy
*
*/
get_header();
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$query = new WP_Query( $args );
$tax = 'product_cat';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
echo '<div class="container">';
echo '<div class="row">';
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '<div class="col-4">';
echo '' . $term->name . ' ';
echo '</div>';
}
echo '</div>';
echo '</div>';
?>
</div>
<?php endif;
if ( $query->have_posts() ): ?>
<div class="tagged-posts">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h2><a class="<?php echo $term->name; ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="tagged-posts">
<h2>No posts found</h2>
</div>
<?php endif; ?>
</article>
<?php
get_footer();
jQuery(document).ready(function( $ ) {
$('.tax-filter').click( function(event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
var selecetd_taxonomy = $(this).attr('title');
$('.tagged-posts').fadeOut();
data = {
action: 'filter_posts',
afp_nonce: afp_vars.afp_nonce,
taxonomy: selecetd_taxonomy,
};
$.ajax({
type: 'product',
dataType: 'json',
url: afp_vars.afp_ajax_url,
data: data,
success: function( data, textStatus, XMLHttpRequest ) {
$('.tagged-posts').html( data );
$('.tagged-posts').fadeIn();
console.log( textStatus );
console.log( XMLHttpRequest );
},
error: function( MLHttpRequest, textStatus, errorThrown ) {
console.log( MLHttpRequest );
console.log( textStatus );
console.log( errorThrown );
$('.tagged-posts').html( 'No posts found' );
$('.tagged-posts').fadeIn();
}
})
});
});
In your query args you are filtering by tag whereas the taxonomy term that you're passing in is actually a product_cat.
Try changing your query arguments in ajax_filter_get_posts to the following:
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
'tax_query' => array(
array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $taxonomy,
)
),
);
I'm having problems to get author_id in this code. Don't show nothing. Some can help me?
Entire code: (I'm trying get custom_post_type value of the same author of post.
<?php global $query_string;
query_posts( $query_string . '&orderby=rand' ); ?>
<?php while(have_posts()): the_post();
$author = get_the_author();
echo 'Autor:'.$author;
$ofertas = array();
// Second Loop
$i = 0;
$args = array(
'author' => $author_id,
'post_type' => 'ofertas'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
$ofertas[$i]['title'] = get_the_title(); // or anything you need
$i++;
endwhile; // close the
else: // if it has no post
continue; // we don't display the post
endif;
wp_reset_postdata();
?>
<?php
$blogusers = get_users('role=author');
foreach ($blogusers as $user) {
echo 'Author ID: '.$user->ID;
$author_id = $user->ID;
$ofertas = array();
// Second Loop
$i = 0;
$args = array(
'author' => $author_id,
'post_type' => 'ofertas'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
$ofertas[$i]['title'] = get_the_title(); // or anything you need
$i++;
endwhile; // close the
else: // if it has no post
continue; // we don't display the post
endif;
wp_reset_postdata();
} // end foreach
?>
Does this suit your requirements ?