Ordering WP_Query results - php

The below query works if I manually assign the meta value like so 'meta_value' => '4-5', but I need it to return posts for each of the $cartArr values. How can something like that be done?
<ul id="order-box" class="list-group">';
print_r($cartArr); //Array ( [0] => 3-6 [1] => 4-5 )
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key ';
}
add_filter('posts_groupby', 'query_group_by_filter');
$the_query = new WP_Query(array(
'post_type' => 'clasa',
'post_status' => 'publish',
'meta_key' => 'twin_id',
'meta_value' => '4-5'
)
);
remove_filter('posts_groupby', 'query_group_by_filter');
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li class="list-group-item" id="remli-'.$post->ID.'">' . get_the_title() . '<a class="right remove" id="remove-'.get_post_meta($post->ID, 'twin_id', true).'" href="#">x</a>';
}
} else {
// no posts found
}
/* Restore original Post Data */
//wp_reset_postdata();
echo '</ul>';

Need to add a foreach loop. like foreach($cartArr as $cartArrr) which is mentioned in bellow line. If working please vote me.
<ul id="order-box" class="list-group">';
<?php
print_r($cartArr); //Array ( [0] => 3-6 [1] => 4-5 )
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key ';
}
add_filter('posts_groupby', 'query_group_by_filter');
foreach($cartArr as $cartArrr) {
$the_query = new WP_Query(array(
'post_type' => 'clasa',
'post_status' => 'publish',
'meta_key' => 'twin_id',
'meta_value' => $cartArrr
)
);
remove_filter('posts_groupby', 'query_group_by_filter');
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li class="list-group-item" id="remli-'.$post->ID.'">' . get_the_title() . '<a class="right remove" id="remove-'.get_post_meta($post->ID, 'twin_id', true).'" href="#">x</a>';
}
} else {
// no posts found
}
/* Restore original Post Data */
//wp_reset_postdata();
}
echo '</ul>';

Related

How do I apply my filter only to one store/url?

I've been struggling applying filters to my woocommerce shop. I have a website with multiple store pages and I've found a filter to only shop products published within the last 30 days. I want this filter to only apply to the 'new releases' shoppage, not to the whole website. Currently it's filtering everything, so even pages created before last month won't appear for example.
How do I make sure this filter is only applied to 1 store? Can I apply an IF statement and check current URL before the filter works or something?
Any help is appreciated.
Filter: (from https://www.sitepoint.com/community/t/show-latest-30-days-products-in-woocommerce/258276)
function baba_recent_products() {
//return 'This is where the recent products should show up if I get the shortcode working. ';
global $woocommerce_loop;
extract( shortcode_atts( array(
'per_page' => '48',
'columns' => '2',
'orderby' => 'date',
'order' => 'desc'
), $atts ) );
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => $meta_query
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'baba_recent_products', 'baba_recent_products' );
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
function baba_recent_products() {
//return 'This is where the recent products should show up if I get the shortcode working. ';
global $woocommerce_loop;
extract( shortcode_atts( array(
'per_page' => '48',
'columns' => '2',
'orderby' => 'date',
'order' => 'desc'
), $atts ) );
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => $meta_query
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'baba_recent_products', 'baba_recent_products' );
function filter_where($where = '') {
global $post;
if($post->ID === /*Page ID as int here*/){
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
}
return $where;
}
add_filter('posts_where', 'filter_where');

How can I add navigation links below WordPress posts using PHP?

I am working on a PHP/WordPress-based website and would like to add 3 navigation links (one previous link and 2 next links) below each post of a custom post type. The links should look like this:
[prev] [next] [next]
My current code is below, but it is not working at the moment. How can I get this code to display the desired links?
$currentID = get_the_ID();
$args = array (
'post_type' => array( 'guides' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'ignore_sticky_posts' =>true,
'post__not_in' => array($currentID)
);
$guides_query = new WP_Query( $args );
if ( $guides_query->have_posts() ) {
echo '<div class="grid-container nxt-article">';
echo '<div class="nxt-article-heading"><h2>Next article</h2></div>';
while ( $guides_query->have_posts() ) {
$guides_query->the_post();
echo '<div class="nxt-article-box"><div class="nxt-article-box-shadow">';
echo '<a class="nxt-article-link" href="'.get_the_permalink().'"></a>';
$gear_category = get_field('gear_category', $products->ID);
if ($gear_category) :
echo '<div class="g_pre_title" style="color:#fff;position:absolute;left:30px;top:25px;">'.$gear_category[0].'</div>';
endif;
echo '<div>';
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'post_grid_image' );
}
echo '</div><div style="padding: 0px 30px 30px 30px;"><div class="nxt-article-box-title">'.get_the_title().'</div>';
$intro_section = get_field('introduction_section', $post->ID);
if( $intro_section ) :
echo '<div class="pg_excerpt" style="height:85px;overflow:hidden;">'.wp_trim_words( $intro_section['wysiwyg'], 32, '...' ).'</div>';
endif;
echo '</div></div></div>';
}
echo '<div style="text-align:center;"><a class="ml_button_style" href="">Show ALL GUIDES</a></div>';
echo '</div>';
} else {
}
wp_reset_postdata();

Retrieve posts by custom taxonomy child on parent taxonomy page

So from the parent taxonomy view:
Child 1
post
post
post
Child 2
post
post
post
post
Child 3
post
post
post
post
I've scoured the internet for a solution to this, but nothing seems to be working. I'm able to successfully echo the term ID, but when I pass that into the query it returns nothing.
<?php
$terms = get_terms( 'ht_kb_category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
if ($term->parent != 0) {
echo '<li><h1>' . $term->name . '</h1></li>';
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array (
'taxonomy' => 'ht_kb_category',
'field' => 'slug',
'terms' => $term->slug,
)
),
) );
while ( $the_query->have_posts() ) :
echo '<p>'. the_title() .'</p>';
endwhile;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
}
}
echo '</ul>';
} ?>
Finally found the solution after some detective work using the custom taxonomy URL. I was missing the 'post_type' and needed to query 'tag_id' instead of 'term_id'.
<?php $current_term_id = $hkb_current_term_id ?>
<?php
$myposts = get_posts(array(
'showposts' => -1,
'post_type' => 'ht_kb',
'tax_query' => array(
array(
'taxonomy' => 'ht_kb_category',
'field' => 'tag_id',
'terms' => $current_term_id)
))
);
echo '<ul>';
foreach ($myposts as $mypost) { ?>
<li><?php echo $mypost->post_title ?></li>
<?php }
echo '</ul>';
?>

How do I create a while loop within a PHP string?

I am creating a query using shortcodes ultimate lightbox. But the only way this will work within a regular php page, is to save the data as string. So what I need to do is to create my query but somehow get my results within a string.
Here is what works before I use any kind of php query:
<?php
$my_tabs = "<ul class='easybuttons'>
<li>[su_lightbox type='inline' src='#lightbox1']AT&T[/su_lightbox]</li>
<li>[su_lightbox type='inline' src='#lightbox2']Sprint[/su_lightbox]</li>
<li>[su_lightbox type='inline' src='#lightbox3']T-Mobile[/su_lightbox]</li>
</ul>";
echo do_shortcode( $my_tabs );
?>
but I need the ATT, Sprint, T-Mobile to be dynamic. Keep in mind the shortcode will only work if it within a string.
So how can I do a while loop within this string? I tried using an operator but did not work.
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'the_title', 'order' => 'ASC');
query_posts($args);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox1"]' . get_the_title() . '</li>';
}
$my_tabs .= '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
echo do_shortcode( $my_tabs );
?>
UPDATE:
I tried using this code but it does work. Nothing comes through. I don't get any errors but no shortcode is displayed.
<?php
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'the_title', 'order' => 'ASC');
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$lid = 1;
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox' . $lid . '"]' . get_the_title() . '</li>';
$lid++;
}
$my_tabs .= '</ul>';
}
echo do_shortcode( $my_tabs );
wp_reset_postdata();
You need to initialise the variable $my_tabs somewhere, for instance outside the if block, and increment the lightbox id. You don't need to call query_posts(). order_by should be title, not the_title. Make sure $childid is definitely a string of the category slug, not the name, if in doubt, take out that parameter altogether to see if you get anything as I imagine this is most likely to be the main issue.
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'title', 'order' => 'ASC');
// The Query
$the_query = new WP_Query( $args );
$my_tabs = '';
// The Loop
if ( $the_query->have_posts() ) {
$lid = 1;
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox' . $lid . '"]' . get_the_title() . '</li>';
$lid++;
}
$my_tabs .= '</ul>';
}

orderby menu_order show max 10 pages

trying to put together a wp site for a martial arts centre, and they need a list of their instructors.
i try to get all the children of the instructor page appear in list by their page order, and that works fine, except it only shows the first 10 pages, and there is currently 13 instructor pages...
this is the code i used:
<section id="instruktorer">
<div class="indent">
<?php
$query = new WP_Query( 'pagename=instruktorer' );
$services_id = $query->queried_object->ID;
/* Restore original Post Data */
wp_reset_postdata();
$args = array(
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$services_query = new WP_Query( $args );
// The Loop
if ( $services_query->have_posts() ) {
echo '<ul class="instruktorer">';
while ( $services_query->have_posts() ) {
$services_query->the_post();
echo '<li class="clear">';
echo '<a href="' . get_permalink() . '" figure class="instruktorer-thumb">';
the_post_thumbnail('instructor-pic');
echo '</a>';
echo '</figure>';
echo '<div class="caption">' . get_post( get_post_thumbnail_id() )->post_excerpt . '</div>';
echo '</li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</div><!-- .indent -->
</section>
Change your query to this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC'
);

Categories