Removing duplicate data from Array loop - php

Currently I am trying to remove duplicate iterations from my post loop, and it seems that my code is working perfectly the only issue is that my &posts_per_page=21 declare is no longer present, it displays only 3 results for example (which are the unique titles)
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'orderby' => 'title'
);
$the_query = new WP_Query( $args );
$unique_artist = array();
query_posts($query_string.'&cat=7&posts_per_page=21'); if ( have_posts() ) : while ( have_posts() ) : the_post();
// title example for post "Artist - Song Name"
$title = get_the_title();
$artist = explode('-', $title);
$main_artist = $artist[0];
$aArtist = $main_artist;
if( ! in_array( $aArtist, $unique_artist ) ) :
$unique_artist[] = $aArtist;
the_title();
endif; endwhile; endif;
How can I make it so that no matter what, my post_per_page will display the 21 desired results? Instead of only showing the unique titles, when we remove the duplicates..

Related

Add the sum of a variable inside a wordpress while loop

I am using wordpress and ACF for a custom field that has a number in it on each post.
I am trying to query a set of posts and then get the sum of the custom field of the posts queried.
$total_income = 0;
$args = array(
'post_type' => 'acct_income',
'year' => '2023',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$acct_amount = the_field('acct_income_amount');
$total_income += $acct_amount;
echo $total_income;
endwhile;
endif;
This just displays the custom field number in each post and doesn't add them together.

How to increment posts within a specific category

I am returning all posts on the page. Every six posts I want to incrementally show a post from a specific category. Every time this comes to pass I would like to increment up in the loop so there are no repeats of the same post within that specific category on the page.
I've successfully gotten the posts from that category showing on every sixth post on the page. I am just unable to get the loop to work so that it shows incrementally the next post in the category on every sixth spot on the page. Currently it just shows the same first post in the array.
<?php while ($query->have_posts()) {
if( $query -> post_count > 0 ) {
$postnum = 0;
foreach( $query -> posts as $post ) {
$postnum++;
if( $postnum%5 == 0 ) {
$args = array( 'cat' => 1824, 'posts_per_page' => 1, );
query_posts( $args );
$current_post = 0;
while ( have_posts() ) : the_post();
$current_post++;
echo "CTA Card Specific Info";
endwhile;
}
$query->the_post();
?>```
Instead of using query_posts(), you could nest anther WP_Query inside the first and use the offset parameter to skip over the posts you've already output. I haven't tested this code, but something like the following could work:
$post_count = 0;
$category_count = 0; // for determining offset
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'category__not_in' => 1824, // or something like this to prevent duplicates
);
$post_query = new WP_Query ( $args );
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) : $post_query->the_post();
$post_count++;
echo "Regular Post Here";
if ( $post_count % 6 === 0 ) {
$args = array(
'cat' => 1824,
'posts_per_page' => 1,
'offset' => $category_count,
);
$category_query = new WP_Query( $args );
$category_count++;
if ( $category_query->have_posts() ) {
while ( $category_query->have_posts() ) : $category_query->the_post();
echo "CTA Card Specific Info";
endwhile; $post_query->reset_postdata();
}
}
endwhile;
}
Once you've finished with the inside loop, make sure you call reset_postdata() to change the context of the query back to the main query.
It's also worth noting that using offset can mess up your pagination. I don't think that will come into play here, but if you notice pagination issues that could be the culprit.

Wordpress woocommerce multiple orderby args loop

Does anyone know how to incorporate a while loop into a basic order by filter? I have the following basic filter in my functions.php page that orders my products by the featured products and it works:
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args = array(
'orderby' => array( 'meta_value' => 'DESC' ),
'meta_key' => '_featured'
);
return $args;
}
}
My problem is I want to use some more advanced while loops to try and get multiple different arguments tied into the same args. I am trying to show my featured products first, then load that into an array then get my next loop and only show the products that are 6 months old or younger and add that to my array, then show everything else and sort them by menu_order and title and add it to my array as shown below:
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $my_post_array ) {
if(!$_GET['orderby']) {
//First loop - show featured
$args['orderby'] = array( 'meta_value' => 'DESC' );
$args['meta_key'] = '_featured';
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Second loop - Show newer than 6 months
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'date_query' => array(
array(
'before' => '6 months ago',
),
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Third loop - show everything else and sort by menu_order and title
$args['orderby'] = array( 'menu_order' => 'ASC', 'title' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Remove duplicate entries from the array
array_unique ( $my_post_array, SORT_STRING );
}
}
Then the last thing I do is remove my duplicates. Only problem is it does not work and I am not sure where I am going wrong, I have not done something like this before so I could be working in the wrong area or missing something obvious. Anyone have ideas on how I can trouble shoot this or does anyone see something that may be causing me issues? Thanks for the help, still learning and trying to improve!

How To Display Categories and the posts inside of a Custom Post Type

I need some help here as I've exhausted every place I can trying to find information. This is what I'm trying to do:
I have created a custom Post type in my admin called "Classes"
That works fine, the data works great and it's inputting in the admin.
I want to make a custom template to show this custom post type. However, everything I try it's not displaying properly. I've tried many code variations.
I know someones already done this and has the block of code to display this. This is what I need the code to do:
List All categories in my custom post type 'classes'
List all posts (show all content, not a link or excerpt) inside of each category.
Display it as such (I'm using Jquery Accordion)
the_category()
the_title()
the_content()
========================================================
By the way, Here is the block of code I'm currently using. It does work, but it ONLY shows the posts, all of them. It does not show the category with posts inside of them.
<?php
$type = 'classes';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h3 class="acc1">';
the_title();
echo '</h3>';
echo '<div class="sc"><div class="vs">View Schedule</div>';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
?>
Community, I need you. Please give your feedback!
What you want to do is actually start with a category query. You have to make sure you query all your categories with your custom post type:
Then for each category you would do pretty much what you have above.
$taxonomy = 'classes';
$args = array('hide_empty' => false,);
$terms = get_terms( $taxonomy, $args );
foreach($terms as $val) {
$term_id = $val->term_id;
$term_name = $val->name;
// now do post query
}
You most likely would have to display the category name as a header for your accordion as well.
Here's all the args for get_terms:
http://codex.wordpress.org/Function_Reference/get_terms
For that query you also most likely have to use a Simple Taxonomy Query (search for that on the page).
http://codex.wordpress.org/Class_Reference/WP_Query
By adding this arg to your above query:
'tax_query' =>
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $term_name )
)
Is that what you were looking for?
There might be a better way to do this but I just had to recently do this and did pretty much what I just outlined here.
I should have been more clear and said to put the posts query within the foreach of the terms query.
Here's the updated answer based on your last reply (I have not tested this).
<?php
$taxonomy = 'classes';
$args = array('hide_empty' => false,);
$terms = get_terms( $taxonomy, $args );
foreach($terms as $val) {
$term_id = $val->term_id;
$term_name = $val->name;
$type = 'classes';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1,
'tax_query' =>
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $term_name )
)
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h3 class="acc1">';
the_title();
echo '</h3>';
echo '<div class="sc"><div class="vs">View Schedule</div>';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
}
?>

How do I check if page ID matches ID in array during loop?

I've written a custom query in WordPress that loops through 4 different page ID's and pulls out the page titles. What I need to do is check if the page being viewed is one of those ID's and if that's the case don't display that specific title. I know I basically need to do a check against the current page ID and the ID's in the array as it loops, but how would I go about doing that?
<?php
$service_args = array (
'post_type'=> 'page',
'post__in' => array(87,106,108,110), // The page ID's
'orderby' => 'ID',
'order' => 'ASC'
);
$servicesquery = new WP_Query( $service_args );
if ( $servicesquery->have_posts() ) {
while ( $servicesquery->have_posts() ) {
$servicesquery->the_post();
?>
<h4><?php echo the_title(); ?></h4>
<?php } wp_reset_postdata(); ?>
You can get the current page/post Id using <?php get_the_ID(); ?>. Find current page id and exclude it from the array that you are preparing.
$posts_array = array(87,106,108,110);
$current_page_id = get_the_ID();
if ( ($key = array_search($current_page_id, $posts_array)) !== false) {
unset($posts_array[$key]);
}
$service_args = array (
'post_type'=> 'page',
'post__in' => $posts_array, // The page ID's array
'orderby' => 'ID',
'order' => 'ASC'
);
$servicesquery = new WP_Query( $service_args );
if ( $servicesquery->have_posts() ) {
while ( $servicesquery->have_posts() ) {
$servicesquery->the_post();
?>
<h4><?php echo the_title(); ?></h4>
<?php
}
wp_reset_postdata();
?>
Try declaring the page id outside of the while loop, like this:
var thisPageId = get_the_ID();
while ( $servicesquery->have_posts() ) {
if ( $servicesquery->post->ID != thisPageId ) {
echo the_title();
}
}
I managed to solve my problem with the help of this post by using array_diff to check against the ID's: https://wordpress.stackexchange.com/questions/108697/use-post-in-and-post-not-in-together
$this_post = $post->ID; // Get the current page ID
$exclude = array($this_post); // Exclude the current page ID from loop
$include = array(87,104,106,108,110); // ID's of pages to loop through
$service_args = array (
'post_type' => 'page',
'post__in' => array_diff($include, $exclude),
'orderby' => 'ID',
'order' => 'ASC'
);

Categories