I am losing the post id when I am doing a wp-query. I need to have it where I am inserting a record, and before if it finds an existing record, as I am using it to find custom fields attached to the post. I am using a page template here and advanced custom fields.
Post Id being lost
<?php
/**
* Template Name: Well Being Page Template
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
?>
<?php get_header(); ?>
<?php
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
// args
$args = array(
'numberposts' => -1,
'post_type' => 'wellbeing',
'post_status' => 'publish',
'meta_key' => 'ip',
'meta_value' => $ip
);
// query
$the_query = new WP_Query( $args );
// I HAVE THE POST ID HERE SO THAT I CAN REFERENCE IT IF THERE IS A RECORD FOUND USE THE POST ID FROM THE POST ?
$isFound=false;
?>
<?php
$count = $the_query->found_posts;
while ( $the_query->have_posts() ) : $the_query->the_post();
if(count==1)
{
$dateJoined = new DateTime($dateJoined);
$expiredate = new DateTime($expiredate);
$diff = $dateJoined->diff($expiredate);
echo 'Days to end'.$diff->days;
}
endwhile;
if ($count==0)
{
// Gather post data.
$my_post = array(
'post_title' => 'Opt In',
'post_content' => 'This is my post.',
'post_type' => 'wellbeing',
'meta_key' => 'ip',
'meta_value' => $ip,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database.
$post_id = wp_insert_post( $my_post );
// I HAVE THE POST ID HERE OK BUT I ALSO NEED IT ABOVE AT THE WP_QUERY
$date = new DateTime();
$date->modify('+2 week');
$currentdate = date('m/d/Y h:i:s', time());
// save a basic text value
$field_key = "expirey_date";
$twoweeksaway = $date->format('m/d/Y h:i:s');;
update_field( $field_key, $twoweeksaway, $post_id );
// save a basic text value
$field_key = "date_joined";
$value = $currentdate;
update_field( $field_key, $value, $post_id );
$current_user = wp_get_current_user();
// save a basic text value
$field_key = "ip";
$value = $ip;
update_field( $field_key, $value, $post_id );
// save a basic text value
$field_key = "email";
$value = wpse_email();
update_field( $field_key, $value, $post_id );
$dateJoined=the_field('date_joined', $post_id );
$expiredate=the_field('expirey_date', $post_id );
$dateJoined = new DateTime($dateJoined);
$expiredate = new DateTime($expiredate);
$diff = $dateJoined->diff($expiredate);
echo 'Days to end'.$diff->days;
}
?>
<?php
echo 'Post Id'.$post_id;
$dateJoined=the_field('date_joined', $post_id );
$expiredate=the_field('expirey_date', $post_id );
//Convert them to timestamps.
$difference=(int)abs((strtotime($expiredate) - strtotime($dateJoined))); // 3
?>
<STYLE>
.myCheckbox input {
// display: none;
// Better than display: none for accessibility reasons
position: relative;
z-index: -9999;
}
.myCheckbox span {
width: 20px;
height: 20px;
display: block;
background: url("link_to_image");
}
.myCheckbox input:checked + span {
background: url("link_to_another_image");
}
</STYLE>
<div style="clear:both;">
Welcome this page allows you to opt out of our well being program. You have <?php echo $difference.'day(s) to opt out click opt out button below';?>
Do you wish to opt out of the well being program
</div>
<div class="form-group">
<form name="wellbeing" action="<?php echo get_permalink(); ?>" method="post">
<input type="submit" value="Opt Out"/>
</div>
</form>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php get_footer(); ?>
Related
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();
}
I am trying to display users attached to a Custom Post Type. I created an ACF-Users-Field which returns the ID. This field is displayed at my Custom Post Type. Now I would like to display the names of the users selected in this field.
I am able to display all names of my users (if I remove the part 'meta_query' of my arguments, but if i add the meta_query array to my arguments nothing is displayed. I am not sure, maybe the mistake is the 'value', but I have no idea what to change there. Below I show the relevant code:
<article>
<?php
$args = array(
'role' => 'Subscriber',
);
$my_user_query = new WP_User_Query( $args );
$editors = $my_user_query->get_results();
if ( ! empty( $editors ) ) {
foreach ( $editors as $editor ) {
$editor_info = get_userdata( $editor->ID );
$args = array(
'post_type' => 'projekt',
'meta_query' => array(
array(
'key' => 'projektteilnehmer', // name of custom field - return ID
'value' => $post->ID ,
'compare' => 'LIKE'
)
)
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) { ?>
<?php foreach ( $user_query->results as $user ) {
echo $user->user_firstname;;
}
} else {
echo 'nothing found';
}
} // endforeach
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
Any help appreciated
By what your describing this code should do what you want. given that the participant's are returned as userID
<article>
<?php
$teilnehmers = get_field('projektteilnehmer', $post->ID);
if ( ! empty( $teilnehmers ) ) {
foreach ( $teilnehmers as $teilnehmer ) {
$user = get_user_by('ID', $teilnehmer);
if($user){
echo $user->user_firstname;;
}
}
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
or if they are returned as user objects
<article>
<?php
$teilnehmers = get_field('projektteilnehmer', $post->ID);
if ( ! empty( $teilnehmers ) ) {
foreach ( $teilnehmers as $teilnehmer ) {
echo $teilnehmer->user_firstname;;
}
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
The answer of Jasper B pushed me in the right direction. Thank you very much:) It is working with this query:
<?php // projekte
$projekte = get_posts(array(
'post_type' => 'projekt',
'meta_query' => array(
array(
'key' => 'projektteilnehmer', // name of custom field - return value id
'value' => $editor_info->ID,
'compare' => 'LIKE'
)
)
));
?>
<?php if( $projekte ): ?>
<strong>Projekte:</strong>
<ul>
<?php foreach( $projekte as $projekt ):?>
<li>
<a href="<?php echo get_permalink( $projekt->ID ); ?>">
<?php echo get_the_title( $projekt->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
// ende projekte ?>
Maybe this questions already asked but that not helpful for my question.
Am creating a API for my WordPress project. So i want to send API for to get all products with pagination.
I get products list using this code:
android/all_products.php
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
I got all products, but i want to show products with pagination.
Note: all the API's are written in inside android folder.
Thanks in advance.
below pagination code is to show next products you can also do it to show previous product by applying same method
if(isset( $_GET['page_num'] ) ) {
$page_number = $_GET['page_num'];
} else {
$page_number = 2;
}
$args = array (
'limit' => 3,
'page' => $page_number,
);
$products = wc_get_products( $args );
foreach( $products as $product ) {`enter code here`
//your data here
// e.g $product_name = $product->get_name();
}
Next
If you want to create API's then just use Woocommerce rest API's which are built in function given in wordpress please use below url it will give you list of products with lots of options:
{{your_url}}/wp-json/wc/v2/products
Go to wp-admin and go to Woocommerce -> settings -> API
-> Enable rest API
-> Go to Key/Apps and create Auth Keys both secret and Private (Copy that keys)
Then give this above url to add it for use in application with o_auth of secret key and private key.
Please see this link for all woocommerce API's : https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-product
Finally i creating a code and send the date using API.
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$brand_product_list = new WP_Query( $args);
$pagination_count = 1;
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$pagination_count++;
endwhile; wp_reset_query();
//echo'<pre>';print_r($pagination_count/12); exit;
wp_reset_query();
?>
<?php
$pagination = round($pagination_count/12);
for($i=1;$i<=$pagination;$i++)
{
$pagination_no[] = $i;
?>
<!-- <a class="product-category-view-all" href="?pagination=<?php echo $i; ?>"><?php echo $i; ?></a> -->
<?php } ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : $_GET['pagination'];
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 12,
);
$wp_query = new WP_Query($args);
while($wp_query->have_posts()) : $wp_query->the_post();
$product_data = wc_get_product( $post->ID );
if(!empty(get_the_post_thumbnail_url($product_data->get_id())))
{
$img = get_the_post_thumbnail_url($product_data->get_id());
}
else
{
$img = "";
}
$product_list[] = array(
'product_id' => $product_data->get_id(),
'product_name' => $product_data->get_title(),
'product_regular_price' => $product_data->get_regular_price(),
'product_sale_price' => $product_data->get_sale_price(),
'product_price' => $product_data->get_price(),
'img' => $img,
'rating' => $product_data->get_average_rating(),
'stock_quantity' => $product_data->get_stock_quantity(),
'stock' => $product_data->is_in_stock(),
);
endwhile; wp_reset_query();
$data[] = array(
'pagination' => $pagination_no,
'product_list' => $product_list
);
//echo json_encode($peoduct_list, $pagination)
echo json_encode($data)
?>
There are many pages, but how to get paging?
Two columns of content have two paging.
i need paging for Two columns of content.
how to do this?
and if you can tell me how to add the function of the comments, I will thank you even more.
look this:
https://stackoverflow.com/a/45451489/1266305
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'desc');
$blank_posts = array();
$content_post =array();
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
global $post;
$content = get_the_content();
if(empty($content)) {
array_push( $blank_posts, $post);
}else{
array_push( $content_post, $post);
}
endwhile;
endif;
/* blank content posts */ /* loop */
if(!empty($blank_posts)){
foreach ($blank_posts as $pst) {
echo "<pre>"; print_r($blank_posts);
echo $pst->post_title;
}
};
/* have content posts */
if(!empty($content_post)){
foreach ($content_post as $pst) {
echo '<a href='. $pst->guid .'>' .$pst->post_title. '</a> ';
}
}
I have given the a url path for image in wordpress using maya theme of woocommerce and the image is not showing the image path looks like
<img title="Adidas-H111-2" alt="Adidas-H111-2" class="attachment-shop_thumbnail" src="http://localhost/alphatest/wp-content/uploads/http://localhost/webservice/ap/upload_images/Adidas/Hard/H111/Adidas-H111-2.jpg" style="opacity: 1;">
how can I remove the part
http://localhost/alphatest/wp-content/uploads/
I am using maya theme and woocommerce plugin in wordpress. the full code is like
<?php
/**
* Single Product Thumbnails
*/
global $post, $woocommerce;
?>
<div class="thumbnails">
<?php
$thumb_id = get_post_thumbnail_id();
$small_thumbnail_size = apply_filters('single_product_small_thumbnail_size', 'shop_thumbnail');
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post__not_in' => array($thumb_id),
'post_mime_type'=> 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$attachments = get_posts($args);
if ($attachments) :
$loop = 0;
$columns = apply_filters('woocommerce_product_thumbnails_columns', 3);
foreach ( $attachments as $attachment ) :
if (get_post_meta($attachment->ID, '_woocommerce_exclude_image', true)==1) continue;
$loop++;
$_post = & get_post( $attachment->ID );
$url = wp_get_attachment_url($_post->ID);
$post_title = esc_attr($_post->post_title);
$image = wp_get_attachment_image($attachment->ID, $small_thumbnail_size);
echo '<a href="'.$url.'" title="'.$post_title.'" rel="thumbnails" class="zoom ';
if ($loop==1 || ($loop-1)%$columns==0) echo 'first';
if ($loop%$columns==0) echo 'last';
echo '">'.$image.'</a>';
endforeach;
endif;
?>
</div>
Simply add Double Slash (//)Instead of the Domain name. Example:
http://www.google.com/wp-content/uploads/2013/10/image.png
will be as:
//wp-content/uploads/2013/10/image.png