Custom Wordpress Query Posts based on meta values - wordpress

I have a simple wordpress query that I need to write as part of a theme I am creating.
I have most of it but need a little push in the right direction to filter the returned results pragmatically based upon a custom meta field of the custom post type.
If the post meta field 'completion date' or 'published date' is equal to or before the current date, put posts into Published Research table.
If the posts meta field 'completion date' or 'published date' is a date ahead of the current date, put the posts into Ongoing Research.
* UPDATED *
Currently I have this:
<div class="container main-content">
<div class="row">
<h1>ONGOING RESEARCH</h1>
<table class="table table-bordered">
<tr>
<th>Type</th>
<th>Title</th>
<th>Summary / Abstract</th>
<th>Keywords</th>
<th>Type of Research</th>
<th>Principle Investigator</th>
<th>Completion Date</th>
</tr>
<?php $wp_query_ongoing = new WP_Query( array( 'post_type' => 'research', 'posts_per_page' => 5, 'order' => 'asc' ) ); ?>
<?php while ( $wp_query_ongoing->have_posts() ) : $wp_query_ongoing->the_post(); //start of the loop ?>
<?php
$post_id = get_the_ID();
$completion_date = get_post_meta( $post_id, "duration_end", true );
$publication_date = get_post_meta( $post_id, "publication_date", true );
?>
<?php if (strtotime($publication_date) >= strtotime("now") || strtotime($completion_date) >= strtotime("now")) { ?>
<tr>
<td><?php echo do_shortcode('[wpuf-meta name="_hidden_type"]' ); ?></td>
<td><?php the_title(); ?></td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>
<?php echo do_shortcode('[wpuf-meta name="duration_end"]'); ?>
<?php echo do_shortcode('[wpuf-meta name="publication_date"]'); ?>
</td>
</tr>
<?php } ?>
<?php endwhile; //end of the loop ?>
</table>
<h1>PUBLISHED RESEARCH</h1>
<table class="table table-bordered">
<tr>
<th>Type</th>
<th>Title</th>
<th>Summary / Abstract</th>
<th>Keywords</th>
<th>Author</th>
<th>Type of Research</th>
<th>Open Access</th>
<th>Date Published</th>
</tr>
<?php $wp_query_published = new WP_Query( array( 'post_type' => 'research', 'posts_per_page' => 5, 'order' => 'asc' ) ); ?>
<?php while ( $wp_query_published->have_posts() ) : $wp_query_published->the_post(); //start of the loop ?>
<?php
$post_id = get_the_ID();
$completion_date = get_post_meta( $post_id, "duration_end", true );
$publication_date = get_post_meta( $post_id, "publication_date", true );
?>
<?php if ( strtotime($publication_date) <= strtotime("now") || strtotime($completion_date) <= strtotime("now")){ ?>
<tr>
<td><?php echo do_shortcode('[wpuf-meta name="_hidden_type"]' ); ?></td>
<td><?php the_title(); ?></td>
<td>
<?php echo do_shortcode('[wpuf-meta name="synopsis"]'); ?>
<?php echo do_shortcode('[wpuf-meta name="abstract"]'); ?>
</td>
<td>
<?php echo do_shortcode('[wpuf-meta name="keywords"]'); ?>
<?php
$terms = get_the_terms($post->ID, 'keywords');
foreach ($terms as $keyword) {
$myKeywords[] = $keyword->name;
}
echo implode( ', ', $myKeywords );
$myKeywords = null;
?>
</td>
<td><?php echo do_shortcode('[wpuf-meta name="author"]' ); ?></td>
<td><?php echo do_shortcode('[wpuf-meta name="type_of_research"]'); ?></td>
<td><?php echo do_shortcode('[wpuf-meta name="open_access"]'); ?></td>
<td>
<?php echo do_shortcode('[wpuf-meta name="duration_end"]'); ?>
<?php echo do_shortcode('[wpuf-meta name="publication_date"]'); ?>
</td>
</tr>
<?php } ?>
<?php endwhile; //end of the loop ?>
</table>
* New related question *
Currently the ongoing research table works correctly, only showing posts that have a meta date set somewhere in the future. However, the published table is the correct posts BUT also the posts that should only be in ongoing. ????
I have never worked with dates in php. How can I modify the above code to return posts according to the the date provided in a meta field.
N/B we are not concerned with the posts actual published date, only the date set in a custom meta field.
https://www.dropbox.com/s/0hddjypw9adj9qj/29_alcoholresearchdirectory_browse-research_1-1.png?dl=0
Thanks for any pointers, I have not worked with php and dates.

Related

WordPress WooCommerce add new field in each row at /my-account/subscriptions/ page

I am using WordPress with WooCommerce and WooCommerce subscriptions plugins and below is my code in which I have added a custom field called (Mindesk VAR Client User - Dropdown) to show in "Edit Subscription" admin page saving to my custom field based on subscription ID.
This is how it's looking like.
And this is my working code.
<?php
// action triggered when we go to add/edit subscription page
add_action('woocommerce_admin_order_data_after_order_details', 'showWCSubscriptionCustomFields');
add_action('woocommerce_process_shop_order_meta', 'saveWCSubscriptionCustomFields');
function showWCSubscriptionCustomFields($subscription) {
$currentPage = get_current_screen();
// If page is "Edit Subscription" page, then only show
if ($currentPage->action == 'add')
return;
// Getting all the users
$mindeskUsers = getAllUsers();
?>
<br class="clear" />
<p class="form-field form-field-wide">
<label for="mindesk_wc_subscriptions_var_client_user_id">Mindesk VAR Client User:</label>
<?php
$selectedUser = get_post_meta($subscription->get_id(), 'mindesk_wc_subscriptions_var_client_user_id', true);
echo getUsersListSelect('mindesk_wc_subscriptions_var_client_user_id', $selectedUser, $mindeskUsers, 'mindesk_select2');
?>
</p>
<?php
}
function saveWCSubscriptionCustomFields($subscription_id) {
// wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions
update_post_meta($subscription_id, 'mindesk_wc_subscriptions_var_client_user_id', wc_clean($_POST['mindesk_wc_subscriptions_var_client_user_id']));
}
This is working fine for me.
Now I have a custom requirement to add a button something called as Transfer in each row in http:://www.mywebsite.com/my-account/subscriptions/ page.
For example this page somewhere beside Total.
After clicking on that button, the popup should come with a form and I should be able to save a field "Transfer to VAR Client User" based on subscription ID same as I have shown you a working code above with my custom field.
I have tried to do R & D but most of the links suggesting to add custom fields and all in "My Account" page (/my-account) But I want to achieve the same in "my-account/subscriptions" page.
Can anyone guide me how can I achieve this? Any suggestion will be highly appreciated.
Thanks
You can copy the my-subscriptions.php file from the woocommerce-subscriptions/templates/myaccount and add it to your active theme woocommerce folder create folder myaccount and paste my-subscriptions.php. and then modify as per your requirement.
<?php if ( ! empty( $subscriptions ) ) : ?>
<table class="my_account_subscriptions my_account_orders woocommerce-orders-table woocommerce-MyAccount-subscriptions shop_table shop_table_responsive woocommerce-orders-table--subscriptions">
<thead>
<tr>
<th class="subscription-id order-number woocommerce-orders-table__header woocommerce-orders-table__header-order-number woocommerce-orders-table__header-subscription-id"><span class="nobr"><?php esc_html_e( 'Subscription', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-status order-status woocommerce-orders-table__header woocommerce-orders-table__header-order-status woocommerce-orders-table__header-subscription-status"><span class="nobr"><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-next-payment order-date woocommerce-orders-table__header woocommerce-orders-table__header-order-date woocommerce-orders-table__header-subscription-next-payment"><span class="nobr"><?php echo esc_html_x( 'Next payment', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-total order-total woocommerce-orders-table__header woocommerce-orders-table__header-order-total woocommerce-orders-table__header-subscription-total"><span class="nobr"><?php echo esc_html_x( 'Total', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-total order-total woocommerce-orders-table__header woocommerce-orders-table__header-order-total woocommerce-orders-table__header-subscription-total"><span class="nobr"><?php echo esc_html_x( 'Transfer', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-actions order-actions woocommerce-orders-table__header woocommerce-orders-table__header-order-actions woocommerce-orders-table__header-subscription-actions"> </th>
</tr>
</thead>
<tbody>
<?php /** #var WC_Subscription $subscription */ ?>
<?php foreach ( $subscriptions as $subscription_id => $subscription ) : ?>
<tr class="order woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $subscription->get_status() ); ?>">
<td class="subscription-id order-number woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-id woocommerce-orders-table__cell-order-number" data-title="<?php esc_attr_e( 'ID', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_html( sprintf( _x( '#%s', 'hash before order number', 'woocommerce-subscriptions' ), $subscription->get_order_number() ) ); ?>
<?php do_action( 'woocommerce_my_subscriptions_after_subscription_id', $subscription ); ?>
</td>
<td class="subscription-status order-status woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-status woocommerce-orders-table__cell-order-status" data-title="<?php esc_attr_e( 'Status', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_attr( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?>
</td>
<td class="subscription-next-payment order-date woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-next-payment woocommerce-orders-table__cell-order-date" data-title="<?php echo esc_attr_x( 'Next Payment', 'table heading', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_attr( $subscription->get_date_to_display( 'next_payment' ) ); ?>
<?php if ( ! $subscription->is_manual() && $subscription->has_status( 'active' ) && $subscription->get_time( 'next_payment' ) > 0 ) : ?>
<br/><small><?php echo esc_attr( $subscription->get_payment_method_to_display( 'customer' ) ); ?></small>
<?php endif; ?>
</td>
<td class="subscription-total order-total woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-total woocommerce-orders-table__cell-order-total" data-title="<?php echo esc_attr_x( 'Total', 'Used in data attribute. Escaped', 'woocommerce-subscriptions' ); ?>">
<?php echo wp_kses_post( $subscription->get_formatted_order_total() ); ?>
</td>
<td class="subscription-actions order-actions woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions woocommerce-orders-table__cell-order-actions">
<?php echo esc_html_x( 'Transfer', 'Transfer a subscription', 'woocommerce-subscriptions' ); ?>
</td>
<td class="subscription-actions order-actions woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions woocommerce-orders-table__cell-order-actions">
<?php echo esc_html_x( 'View', 'view a subscription', 'woocommerce-subscriptions' ); ?>
<?php do_action( 'woocommerce_my_subscriptions_actions', $subscription ); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if ( 1 < $max_num_pages ) : ?>
<div class="woocommerce-pagination woocommerce-pagination--without-numbers woocommerce-Pagination">
<?php if ( 1 !== $current_page ) : ?>
<a class="woocommerce-button woocommerce-button--previous woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'subscriptions', $current_page - 1 ) ); ?>"><?php esc_html_e( 'Previous', 'woocommerce-subscriptions' ); ?></a>
<?php endif; ?>
<?php if ( intval( $max_num_pages ) !== $current_page ) : ?>
<a class="woocommerce-button woocommerce-button--next woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'subscriptions', $current_page + 1 ) ); ?>"><?php esc_html_e( 'Next', 'woocommerce-subscriptions' ); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php else : ?>
<p class="no_subscriptions woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
<?php if ( 1 < $current_page ) :
printf( esc_html__( 'You have reached the end of subscriptions. Go to the %sfirst page%s.', 'woocommerce-subscriptions' ), '', '' );
else :
esc_html_e( 'You have no active subscriptions.', 'woocommerce-subscriptions' );
?>
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
<?php esc_html_e( 'Browse products', 'woocommerce-subscriptions' ); ?>
</a>
<?php
endif; ?>
</p>
<?php endif; ?>
Tested and works

Change WooCommerce attribute table to two columns

I'm very new to Wordpress/WooCommerce and I'm trying to do something I thought would have been simple.
The very basic WooCommerce Single Product page has the Product Attributes, they all appear under one big column.
Where can I actually modify the style and css for Product Page, so that I can have them in 2 columns?
I see the table is itself is generated under class "woocommerce-product-attributes" and "shop_attributes" ?
Any guidance is very appreciated.
Had the same issue, solved it by editing woocommerce/templates/single-product/product-attributes.php. To make the change permanent between woocommerce updates, copy the file contents to yourtheme/woocommerce/single-product/product-attributes.php.
Change:
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
</table>
to:
<table>
<?php foreach (array_chunk($product_attributes, 2) as $product_attribute_key => $product_attribute) :{ ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<?php foreach ($product_attribute as $value) :{ ?>
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $value['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $value['value'] ); ?></td>
<?php } endforeach; ?>
</tr>
<?php } endforeach; ?>
</table>

how to add br between attributes item woocommerce

hi everyone i want to try add tag on the attributes item woocommerce to show any item per line .
can i help me to do that?
here is my product-attributes.php file code
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
you can try below code for example:
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo str_replace(',','<br>',wp_kses_post( $product_attribute['value'] )); ?></td>
</tr>
<?php endforeach; ?>
I didn't check on a real project. let me know if it works or not.
Please try following code and check if it's working.
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ).'<br>'; ?></td>
</tr>

WooCommerce get variable product attribute label

I'm trying to display the details from my custom product attributes on a table on my single product page. I can get the product variation SKU, but I can't seem to get the "label" (the best I can do is get the slug).
This is the code that I'm currently working with:
<!-- ******* START PRODUCT VARIABLE TABLE SECTION ******* -->
<?php
global $woocommerce, $product, $post;
if( $product->is_type( 'variable' ) ) :
$available_variations = $product->get_available_variations();
// START CHECK IF VARIATIONS ARE PRESENT
if ( $available_variations ) :
$attributes = $product->get_attributes();
?>
<div class="row">
<div class="small-12 column">
<table class="varations-table">
<thead>
<tr>
<th class="small-4">ORDER #</th>
<th class="small-5">DESCRIPTION</th>
<th class="small-3">UOM</th>
</tr>
</thead>
<tbody>
<?php foreach ($available_variations as $prod_variation) : ?>
<?php
// get some vars to work with
$post_id = $prod_variation['variation_id'];
$post_object = get_post($post_id);
?>
<tr>
<td><?php echo $prod_variation['sku']; ?></td>
<?php foreach ($prod_variation['attributes'] as $attr_name => $attr_value) : ?>
<td><?php echo $attr_value; ?></td>
<?php endforeach;?>
<td class="uom"><?php echo the_field( 'uom' ); ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
</div>
<?php
// END CHECK IF VARIATIONS ARE PRESENT
endif;
endif;
?>
<!-- ******* END PRODUCT VARIABLE TABLE SECTION ******* -->
Does anyone know how I can pull the Variable label (not just the slug, replacing dashes with spaces using str_replace() )?
If you have the slug, you can get a WordPress taxonomy object with the get_taxonomy() function. So to get the label of a WooCommerce custom attribute you just have to do get_taxonomy($attribute_slug)->labels->name.
<?php if ( $product->is_type( 'variable' )) : ?>
<div class="size">
<span class="name-size">SIZE</span>
<span class="value-size">
<?php
$attr = get_post_meta($post->ID, '_product_attributes', true);
echo $attr['size']['value'];
?>
</span>
</div>
<?php endif; ?>

How to display the_meta() of a Wordpress custom page in a table

I have some meta values on my wordpress custom page that I need to be displayed in a table. Is there any way I can do that.
Here's the code I'm using now: <?php the_meta(); ?>
And this is what it shows:
I want to do something like:
I found out that:
the_meta() is located in wp-includes/post-template.php
This makes the output:
<ul class='post-meta'>
<li><span class='post-meta-key'>your_key:</span> your_value</li>
</ul>
So it's not recommended to edit files in that folder because of the wordpress update.
<table>
<tr><td colspan="2">Game Summary</td></tr>
<?php
$meta = get_post_meta( get_the_ID() );
$exclude = array('_edit_last', '_wp_page_template', '_edit_lock');
foreach( $meta as $key => $value ) {
if( in_array( $key, $exclude) )
continue;
?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value[0]; ?></td>
</tr>
<?php
}
?>
</table>

Resources