Custom post type exclude taxonomy posts - php

I have a cutom post type named : art_design and i have a taxonomy called artdesign_mainslider, in one loop i have only posts from the taxonomy artdesign_mainslider and in the other loop i have posts from custom post type art_design but i want to exclude the taxonomy ?
1st loop:
<?php
$args = array( 'artdesign_mainslider' => 'art-design-featured-post', 'posts_per_page' => 5 );$loop = new WP_Query( $args );while ( $loop->have_posts() ) : $loop->the_post(); ?><?php endwhile; ?>
2nd loop ( i want to exclude artdesign_mainslider):
<?php
$args = array( 'post_type' => 'art_design', 'posts_per_page' => 10, );$loop = new WP_Query( $args );while ( $loop->have_posts() ) : $loop->the_post(); ?><?php endwhile; ?>

You have to use the tax_query parameter. See http://www.wpexplorer.com/exclude-taxonomy-wordpress/ for a perfect example of what you're trying to do and http://codex.wordpress.org/Class_Reference/WP_Query for the reference for tax_query.

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 get all products from current WooCommerce product category?

Customizing my archive-product.php. How can I show only products within a certain category in a custom loop? This similar question, didn't solve my problem. I tried single_cat_title() to get the current category based on this question but got an error. I think I need to use get_queried_object() based on this documentation but I keep getting errors.
I tried this:
<?php
$category = single_cat_title('', false); // this returns your current category ?>
<?php
// Setup your custom query
$args = array(
'post_type' => 'product',
'product_cat' => $category,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_post_thumbnail(); ?>
<br>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
I also tried:
`$term_name = get_queried_object()->name;`
// Setup your custom query
$args = array(
'post_type' => 'product',
'product_cat' => $term_name, );
Updated
On product category archive pages, to get the current product category term you will use:
Wordpress function get_queried_object() (to get the WP_Term Oject).
or Wordpress function get_queried_object_id() (to get the term Id).
Using directly the taxonomy parameter in a WP_Query is deprecated since WordPress 3.1. Instead you will use a tax query as follow:
<?php
// Get The queried object ( a WP_Term or a WP_Post Object)
$term = get_queried_object();
// To be sure that is a WP_Term Object to avoid errors
if( is_a($term, 'WP_Term') ) :
// Setup your custom query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // The taxonomy name
'field' => 'term_id', // Type of field ('term_id', 'slug', 'name' or 'term_taxonomy_id')
'terms' => $term->term_id, // can be an integer, a string or an array
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div style="margin:8px; text-align:center;">
<a href="'.get_the_permalink().'">';
the_post_thumbnail();
the_title();
echo '</a></div>';
endwhile;
wp_reset_postdata(); // Remember to reset
endif; endif;
?>
Tested and works.
Documentation: WP_Query and Taxonomy Parameters

Filter & display Custom Post Type posts by their assigned catagories?

I have a Custom Post Type called 'Services', it's slug is called 'services'. And as displayed in this image, they all have their own assigned categories.
How do I go about writing a WP_Query/loop that displays the titles of all 'Services' with the category 'Face'? (With a hyperlink to their respective pages also.)
Below is code that will achieve the result you want
$args = array(
'post_type' => 'services',
'category' => 'Face',
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
----Here will be your html in which format you want to display your title-----
<p><?php the_title(); ?></p>
<?php endwhile;
endif; ?>

How to get the link of the_ID();

I got 4 posts in Wordpress.
In those posts they can add a link from custom field.
I got this code:
$args = array( 'post_type' => 'post');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_ID();
endwhile;
$other_page = 1330; // variable
the_field('bloglink', $other_page); // show link
The output is: 104013301196827, #link
(I made an example for $other_page)
My question is: How can I use those id's to make a link?
Any help is appreciated
get_the_ID() - Retrieve the ID of the current item in the WordPress Loop.
<?php
$args = array( 'post_type' => 'post');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_ID();
the_field('bloglink', get_the_ID()); // show link
endwhile;
?>

Show related posts by taxonomy Wordpress

I am trying to show related posts by taxonomy in single post page on Wordpress. I've used the following code to show posts in the same category, but not the same custom taxonomy. The custom taxonomy I need to use is product_cat
<?php
global $post;
$categories = get_the_category();
$category = $categories[0];
$cat_ID = $category->cat_ID;
$loop = new WP_Query( array( 'post_type' => 'product','post__not_in' => array($post->ID), 'category' => $cat_ID ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<?php endwhile; ?>
How can I adjust the current code?
Did you tried adding the following argument to your WP_Query array of arguments?
'tax_query' => array(array('taxonomy' => 'product_cat'))
The code will look like this (of course after removing the category parameter):
<?php
global $post;
$loop = new WP_Query( array( 'post_type' => 'product','post__not_in' => array($post->ID), 'tax_query' => array(array('taxonomy' => 'product_cat'))) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<?php endwhile; ?>

Categories