Only loop through post if ACF field is filled - php

I created a custom post type 'Team' which contains all our team members and some information for each person. I added a shortcode that renders a block with our Spotify-list and a picture of someone with headphones.
The picture is taken from the field 'team_foto_met_koptelefoon', but not everyone has this pictures set. I now want to loop only through the people (posts) that have a picture set in that field and ignore the others so everytime the page is reloaded, the Spotify-block is rendered but every time with a different picture.
I almost got it to work, but sometimes the block disappears, I think because he doesn't show the block if there is no picture set.
function dfib_team_spotify_shortcode( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'team',
'posts_per_page' => 1,
'order' => 'ASC',
'meta_key' => 'team_foto_met_koptelefoon',
'meta_value' => ' ',
'meta_compare' => '!=',
'orderby' => 'rand',
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if( get_field('team_foto_met_koptelefoon') ): ?>
<div class="block__spotify">
<div class="block__spotify__inner" <?php if( get_field('team_foto_met_koptelefoon') ): ?> style="background-image: url(<?php the_field('team_foto_met_koptelefoon'); ?>);"<?php endif; ?>>
<h3 class="block__spotify--title">
<svg xmlns="http://www.w3.org/2000/svg" width="60" height="60" viewBox="0 0 60 60">
<g id="Group_1223" data-name="Group 1223" transform="translate(-1147 -1547.002)">
<g id="Group_1159" data-name="Group 1159" transform="translate(1147 1547.002)">
<path id="Path_779" data-name="Path 779" d="M59.555,188.083a30,30,0,1,0,30,30A30,30,0,0,0,59.555,188.083Zm13.758,43.27a1.869,1.869,0,0,1-2.571.622C63.7,227.67,54.83,226.7,44.388,229.083a1.869,1.869,0,0,1-.833-3.645c11.428-2.613,21.23-1.489,29.138,3.344A1.87,1.87,0,0,1,73.313,231.353Zm3.671-8.169a2.34,2.34,0,0,1-3.217.77c-8.062-4.956-20.356-6.391-29.894-3.5a2.338,2.338,0,1,1-1.36-4.475c10.9-3.306,24.441-1.705,33.7,3.986A2.339,2.339,0,0,1,76.984,223.184Zm.317-8.506c-9.67-5.743-25.621-6.271-34.853-3.469a2.806,2.806,0,1,1-1.629-5.37c10.6-3.217,28.213-2.6,39.346,4.013a2.806,2.806,0,0,1-2.864,4.826Z" transform="translate(-29.555 -188.083)" fill="#fff"/>
</g>
</g>
</svg>
luister naar</br> onze favoriete songs
</h3>
<div class="spotify--player">
<h2>#favoritetunes</h2>
<p>geniet van onze karaktervolle muzieklijst</p>
<iframe src="https://open.spotify.com/embed/playlist/6AZ1LZ8PKbkXkoBopPWDqM" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>
</div>
<div class="spotify--img">
<?php if( get_field('team_foto_met_koptelefoon') ): ?>
<img src="<?php the_field('team_foto_met_koptelefoon'); ?>">
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata(); ?>
<?php
$myvariable = ob_get_clean();
return $myvariable;
}
?>
<?php }

$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'team_foto_met_koptelefoon',
'compare' => 'EXISTS'
)
),
);
$team_foto_met_koptelefoon = get_posts($args);
_e("<pre>");
print_r($team_foto_met_koptelefoon); //Your posts
_e("</pre>");

Related

Is there a way to list all the authors who have published videos in a certain category and show them together, like a list?

Please help me with this website https://desarrollowebtotal.space/yoentrenoencasa/
I'm using goWatch theme from touchsize as parent theme.
What I need is this:
When I click on any of the categories listed on the home page, e.g. Yoga, the site will navigate to an archive with all the videos created in that taxonomy, it is not the behavior I want.
What I want is for that archive to return a list of authors who have published videos in that category and nested, as a list, show the videos associated with that author within that category.
Is it possible to do this?
// This query brings the videos within the videos_categories taxonomy that match the current slug
$term = get_queried_object();
$loop = new WP_Query( array(
'post_type' => 'video',
'posts_per_page' => 10,
'tax_query' => array( //(array) - use taxonomy parameters (available with Version 3.1).
'relation' => 'AND', //(string) - Possible values are 'AND' or 'OR' and is the equivalent of running a JOIN for each taxonomy
array(
'taxonomy' => 'videos_categories', //(string) - Taxonomy.
'field' => 'slug', //(string) - Select taxonomy term by ('id' or 'slug')
terms' => $term->slug, //(int/string/array) - Taxonomy term(s).
include_children' => true, //(bool) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
'operator' => 'IN' //(string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
),
),
) );
This is what I've done so far, I have no idea how to nest the video listings so that the parent is the author and the child the videos associated with that author
//UPDATE 1
$argsA = array(
'orderby' => 'name',
'order' => 'ASC',
'number' => null,
'optioncount' => false,
'exclude_admin' => true,
'show_fullname' => false,
'hide_empty' => false,
'echo' => true,
// 'feed' => [empty string],
// 'feed_image' => [empty string],
// 'feed_type' => [empty string],
'style' => 'list',
'html' => true,
);
$authors = wp_list_authors($argsA);
// This query brings the videos within the videos_categories taxonomy that match the current slug
$term = get_queried_object();
HTML PART WHERE I LOOP THE RESULTS
<div class="container airkit-archive-content">
<div class="row">
<?php if (!empty($authors)): ?>
<?php foreach ($authors as $author): ?>
<?php
$author_id = $author->ID;
$author_display_name = $author->name;
$query_args = array(
'author' => $author_id,
'post_type' => 'video',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'videos_categories',
'field' => 'slug',
'terms' => $term->slug,
'include_children' => true,
'operator' => 'IN'
),
),
);
?>
<?php endforeach; ?>
<?php endif ?>
</div>
</div>
The OUTPUT
Cristian Entrenador
j.b
Jose Miguel
Yonatan T.
What i need
Trainer name
Videos he/she posted in the current category
UPDATE 2:
Doing as #Tokant requested/recommended i copy/pasted his snippet, it worked, it printed an author, i think it's because he's the only one who has posted videos
<div class="row">
<?php $term = get_queried_object(); ?>
<?php $authors_args = array(
'echo' => false,
'orderby' => 'name',
'order' => 'ASC',
);
$author_list = wp_list_authors($authors_args);
if (!empty($author_list)):
echo $author_list;
foreach ($author_list as $author):
$author_id = $author->ID;
$author_display_name = $author->name;
$query_args = array(
'author' => $author_id,
);
$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
endwhile;
wp_reset_postdata();
endif;
endforeach;
endif; ?>
</div>
OUPUTS THIS
Jose Miguel
Good morning guys, thanks to Tokant I was able to find the solution in my own way.
What I did was put the layout together so that the hierarchy would be like this.
Author
- Videos posted in the current category.
This is the solution:
You have to install Buddypress plugin for 'orden' key to work, and add this script to functions.php of your child theme
/*Función para ordenar los autores*/
add_action('show_user_profile', 'extra_user_profile_fields');
add_action('edit_user_profile', 'extra_user_profile_fields');
function extra_user_profile_fields($user)
{ ?>
<?php if (current_user_can('administrator')) { ?>
<h3><?php _e("Administrar Posiciones", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="orden"><?php _e("Modificar Orden"); ?></label></th>
<td>
<input type="number" name="orden" id="orden" value="<?php echo esc_attr(get_the_author_meta('orden', $user->ID)); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Campo solo para administradores"); ?></span>
</td>
</tr>
<tr>
</table>
<?php } ?>
<?php }
add_action('personal_options_update', 'save_extra_user_profile_fields');
add_action('edit_user_profile_update', 'save_extra_user_profile_fields');
function save_extra_user_profile_fields($user_id)
{
if (!current_user_can('edit_user', $user_id)) {
return false;
}
update_user_meta($user_id, 'orden', $_POST['orden']);
}
Loop for the authors:
<?php
// Theme's WP_Query arguments
$airkit_video = '';
$figure_attributes = array();
$airkit_is_img = airkit_single_option('img');
$airkit_img_src =wp_get_attachment_url(get_post_thumbnail_id($videos->ID));
$airkit_video_type = get_post_meta($videos->ID, 'video_type', true);
$airkit_external_video = get_post_meta($videos->ID, 'video_url', true);
$airkit_embedded_video = get_post_meta($videos->ID, 'video_embed', true);
$airkit_uploaded_video = get_post_meta($videos->ID, 'video_upload', true);
//Arguments for users Query
$arg_user = array(
'role__in' => ['author'],
'post_type' => 'video',
'has_published_posts' => ['video'],
'fields' => ['ID'],
'meta_query' => array(
array(
'relation' => 'OR',
array('key' => 'orden', 'value' => '0', 'compare' => '!='),
array('key' => 'orden', 'compare' => 'NOT EXISTS'),
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
//var_dump($arg_user);
$yec_args = get_users($arg_user); //We obtain the list of all the users of the site to compare with the criteria of the query, it will look for in the author role, the users that have published entries in the custom post type video and it will avoid to bring the ones that do not have entries.
$yec_user_id = wp_list_pluck($yec_args, 'ID');
//print_r($yec_user_id);
?>
?>
Then we loop through each author and output the html
<div class="container container-todo" style="transform: none; display: flex;">
<div class="post-details-row current-reading" style="transform: none;">
<?php
// Loop & retrieve el ID authors ID
foreach ($yec_user_id as $author_id): //main FOREACH
// We get the ID of the current author who is returning the loop
$curauth = get_userdata($author_id);
//var_dump($curauth->user_nicename);
$args = array(
'posts_per_page' => -1,
'post_type' => 'video',
'author' => $author_id,
'groupby' => 'author',
'tax_query' => array(
array(
'taxonomy' => 'videos_categories',
'field' => 'slug',
'terms' => get_queried_object()->slug,
),
),
'meta_key' => 'airkit_views',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
//We return the videos and count them.
$posts = get_posts($args);
$number_post = count($posts);
//var_dump($number_post);
//if this author has posts, then include his name in the list otherwise don't
if (isset($posts) && !empty($posts)) { // MAIN IF?>
<div class="row" style="display: flex; align-items: center;">
<div class="sidebar-right col-lg-12 col-md-12 col-xs-12" style="display: flex; justify-content: center; flex-wrap: wrap;">
<!-- Datos del autor -->
<div class="tszf-author-body">
<div class="item col-md-12 col-xs-12" style="text-align: center;">
<div class="yec-user-image">
<img src="<?php echo esc_url(get_avatar_url($author_id)); ?>" />
</div>
<div class="item col-xs-12">
<span class="yec-authors"> <?php echo "" . $curauth->first_name . " " . $curauth->last_name . ""; ?> </span>
</div>
</div>
</div>
<div class="tszf-user-name">
<ul class="social"><?php echo airkit_var_sanitize($social_icons, 'the_kses'); ?></ul>
</div>
<!-- Fin de datos del autor -->
<!-- Aquí deben ir los vídeos de ese autor -->
<?php $count = 0; ?>
<?php foreach ($posts as $post) { ?>
<?php if ($count == "4") {
// Si hay vídeos no imprimas más de 2 y salte del condicional
break;
} else{ ?>
<!-- Este es el bloque iterable -->
<div class="item col-lg-4 col-md-6 col-sm-6 col-xs-12">
<article class="video type-video status-publish has-post-thumbnail hentry has-lazy-placeholder airkit_view-article text-left below-image effect-always hidden-excerpt has-image" itemscope="" itemtype="http://schema.org/Article">
<figure class="image-holder has-background-img lazy lazyloaded" style="background-image: url(<?php echo get_the_post_thumbnail_url($post->ID, 'full') ?>); display: block;">
<a class="post-format-link" href="<?php the_permalink(); ?>" title="<?php echo $post->post_title; ?>">
<span class="post-type"><i class="icon-play-full"></i></span>
</a>
<div class="overlay-effect ">
<a href="<?php the_permalink(); ?>" class="overlay-link darken">
</a>
</div>
</figure>
<header class="entry-content-below">
<div class="entry-content">
<ul class="entry-categories">
<li class="term">
<?php $slug = get_queried_object()->slug; ?>
<?php echo $post->post_title; ?>
<!-- <?php echo $slug; ?> -->
</li>
</ul>
<h2 class="entry-title" itemprop="name headline">
<?php echo $video->post_title; ?>
<div class="widget-meta">
<ul class="list-inline">
<li class="red-comments">
<a href="<?php echo get_permalink($post->ID) . '#comments' ?>">
<i class="icon-comments"></i>
<span class="comments-count">
<?php echo airkit_var_sanitize($post->comment_count, 'the_kses') . ' '; ?>
</span>
</a>
</li>
<li class="meta-views">
<i class="icon-views-1"></i> <?php airkit_get_views($post->ID, true); ?>
</li>
</ul>
</div>
</h2>
</div> <!-- Aquí termina el contenido de los vídeos -->
</header>
</article>
</div>
<!--Fin del bloque iterable -->
<?php $count++; } //CIERRE DEL IF DEL COUNT ?>
<?php } //CIERRE FOREACH DE POST ?>
</div>
<!-- Flecha que nos lleva al perfil del autor -->
<a href=" <?php echo get_option('siteurl') . '/members/' . $curauth->user_nicename . '/posts' ?>" class="post-link">
<span style="font-size: 3em; color: rgba(60,185,207,1);">
<i class="fas fa-angle-double-right" style="margin-top:50px"></i>
</span>
<br />
</a>
<span style="color: rgba(60,185,207,1);"> (<?php echo $number_post; ?>) <i class="fas fa-video"></i> </span>
</div>
<?php } // END of MAIN IF ?>
<?php endforeach; //END OF MAIN FOREACH ?>
You could do a WP_Query for all authors. In the loop for each author you first get the author's ID and then create a WP_Query for the 'video' post type, just like you have already done but adding the authour's ID in the $args, like so:
$authors_args = array(
'echo' => false,
'orderby' => 'name',
'order' => 'ASC',
);
$authors = wp_list_authors($args);
if (!empty($authors)):
foreach ($authors as $author):
$author_id = $author->ID;
$author_display_name = $author->name;
$query_args = array(
'author' => $author_id,
[your args]
);
$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) :
[output author name]
while ( $the_query->have_posts() ) : $the_query->the_post();
[output post/video]
endwhile;
wp_reset_postdata();
endif;
endforeach;
endif;

Woocommerce Product Loop With Categories Filter

I am trying to integrate a woocommerce product loop to use a category filter. Currently it works well if with the normal post categories but I can't get it to work with woocommerce product categories without breaking it. I have commented the parts that are giving me problem in the codes as shown below.
<ul class="portfolio-filter">
<li><a class="active" href="#" data-filter="*">All</a></li>
<?php
$args = array(
'hide_empty'=> 0,
'orderby' => 'name',
'order' => 'ASC'
);
//I want to list woocommerce categories
$categories = get_categories($args);
foreach($categories as $category) {
echo
'<li>
<a href="#" data-filter=".'.$category->name.'">
'.$category->name.'
</a>
</li>';
}
?>
</ul>
<div class="row">
<div class="portfolio-items">
<?php
//i want to replace 'cat' => $category_id with recommended woocommerce cat id
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts()) : $loop->the_post(); global $product;
if ( $attachments = get_children(
array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 99,// -1 to get all images
'post_status' => null,
'post_parent' => $post->ID
)
));
?>
<!--I want to display the product category name as a class to replace $category as shown below-->
<div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php $category = get_the_category( $post->ID );
echo $category[0]->cat_name;?>">
<div class="project_thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="project_cont">
<a href="<?php the_permalink(); ?>">
<h3 class="boxeq"><?php the_title(); ?></h3>
</a>
<button class="btn-green">See Features</button>
<button class="btn-line">Acquire Property</button>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div><!--portfolio-items-->
I was finally able to solve this problem. For the sake of those that face the same problem that might come across this, below is the updated working code for your perusal.
<ul class="portfolio-filter">
<li>
<a class="active" href="#" data-filter="*">All</a>
</li>
<?php
$args = array(
'hide_empty'=> 0,
'orderby' => 'name',
'order' => 'ASC'
);
$product_categories = get_terms( 'product_cat', $cat_args );
foreach ($product_categories as $key => $category) {
echo
'<li>
<a href="#" data-filter=".'.$category->name.'">
'.$category->name.'
</a>
</li>';
}
?>
</ul>
<div class="row">
<div class="portfolio-items">
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts()) : $loop->the_post(); global $product;
if ( $attachments = get_children(
array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 99,// -1 to get all images
'post_status' => null,
'post_parent' => $post->ID
)
));
global $wp_query;
$terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
foreach ($terms_post as $term_cat) {
}
?>
<div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php echo $term_cat->name; ?>">
<div class="project_thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="project_cont">
<a href="<?php the_permalink(); ?>">
<h3 class="boxeq"><?php the_title(); ?></h3>
</a>
<button class="btn-green">See Features</button>
<button class="btn-line">Acquire Property</button>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div><!--portfolio-items-->
</div>

Tag template instead of 'post' in the following loop

I'm using the code (loop) below for the blog post grid displaying. I want the following structure for my tag.php as well (the post by the tag instead of all blog posts). But I don't know how to do it. Could you, please, check it?
I can't remember how I solved the following problem in the past but I'm at a 100% sure that I found the solution previously.
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="item col-sm-4">
<div class="well">
<h2><?php the_title(); ?></h2>
<span class="date"><?php echo get_the_date("j.n.Y"); ?></span>
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Suite</a>
</div>
</div></div>
<?php endwhile; ?>
</div>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="next-posts-link">
<?php echo get_previous_posts_link( '< Page précédente' ); ?>
</div>
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Page suivante >', $custom_query->max_num_pages ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Please check WP_Query with Tag Parameter
https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
example:
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date', // modified | title | name | ID | rand
'tag' => 'tag1,tag2'
);

How to restrict what tags are fetched in WordPress

I have this PHP script to fetch tags (as we use WordPress as a CMS, the tags are "categories" for our partners).... However, I'm trying to free up "tags" to be used on blog posts - but currently the code below fetches ALL tags - how can I restrict this to only tags I specify (happy to enter each Tag ID)
Here is the code:
<?php
$tags = get_tags('order=ASC&orderby=name);
foreach ( $tags as $tag ) { ?>
<?php $tag_name = $tag->slug;
echo '<div class="col-md-12 padbot50" id="tabs-' . $tag->slug . '">';
echo '<div class="green_txt font20">' .$tag->name. '</div>';
//echo $tag_name;
$args = array( 'posts_per_page' => 10, 'cat'=> 11, 'tag' => $tag_name, 'orderby' => 'slug', 'order' => 'ASC' );
query_posts( $args );
// The Loop
?>
<div class="row">
<?php
while ( have_posts() ) : the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'medium', true);
$thumb_url = $thumb_url_array[0];
$post_id = get_the_ID();
?>
<div class="col-md-6 col-sm-5 col-xs-7 pad10 center"> <a href="<?php the_permalink(); ?>">
<div class="partners_container">
<div class="row" style="height:125px;">
<div class="col-md-12 col-xs-12"><div class="left"> <img src="<?php echo $thumb_url; ?>" height="100%" alt="Volo Commerce Partners - Multichannel Automated Back Office Software. eBay, Amazon, Rakuten" title="Volo Commerce Partners - Multichannel eCommerce Software. Stock & Inventory Management"/><div class="partner-more-icon"></div></div></div>
</div>
<div class="padtop10" style="text-align:left;font-weight:bold;color:#f08f00!important;"> <?php echo '<div>' . the_title() . '</div>';?> </div>
<div class="padtop10 left"> <?php echo '<div>' . the_field('partner_page_description', $post_id ) . '</div>';?> </div>
</div>
</a> </div>
<?php
endwhile;
?>
</div>
</div>
<?php
wp_reset_postdata(); }?>
If anyone could be so kind to tell me how I fetch ONLY certain tag ID's - that would be absolutely perfect!
Thanks muchly!
This is a basic example of WP_Query which should get you started:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'slug',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'field' => 11
),
array(
'taxonomy' => 'tag',
'field' => 'term_id',
'field' => array( /* Your Tag IDs */ ),
'operator' => 'IN'
)
)
);
$post_query = new WP_Query( $args );
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) :
$post_query->the_post();
// Inner loop stuff code
endwhile;
endif;
wp_reset_postdata();?>
get_tags function accepts include argument. You could pass by it ids of tags you want to get.
$tags = get_tags('order=ASC&orderby=name');
change to
$tags = get_tags('order=ASC&orderby=name&include=12,45,67');
12, 45 and 67 are ids of tags
get_tags on codex

Displaying a specific category in a WP loop

On my WordPress website, I created a page in which I display 9 posts of the category "interviews". Then there is a button ("Charger plus d'interviews") to click if you want to see more posts of the category.
There is a problem in the loop, because when I click on it, it displays posts from all the other categories.
What shall I do to display only the "interviews" ?
<section class="blocs" id="home_article_derniers">
<div class="iso-container">
<?php
$post_home_query = new WP_Query(array(
'post_type' => 'post',
'category_name' => 'interview',
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'paged' => 2
));
if($post_home_query->have_posts()){
while ( $post_home_query->have_posts() ) {
$post_home_query->the_post();
get_template_part('loop-post', 'actus');
}
wp_reset_postdata();
} ?>
</div>
</section>
<div class="separator">
<a class="btn btn-default more-article" data-paged="1" data-post-not-in="<?php echo implode(',', $array_post_not_in) ?>">Charger plus d'interviews »</a>
?>
</div>
This is the code of the loop :
<?php $color = wami_get_first_category_color(get_the_ID()); ?>
<article data-id="<?php the_ID(); ?>" class="post post-actu iso-item bloc-infinity <?php echo (isset($phone) ? $phone : '' ) ?> ">
<a href="<?php the_permalink(); ?>">
<div class="post-header">
<?php
if(has_post_thumbnail()):
the_post_thumbnail('actu-hp-small');
endif;
?>
<div class="overlay">
<div class="img"></div>
<div class="background" style="background-color:<?php echo $color; ?>">
</div>
</div>
<div class="post-header-title">
<?php wami_the_first_category(get_the_ID(), false, $color, true); ?>
</div>
</div>
<div class="post-body">
<?php
the_title( '<h2>', '</h2>' );
/*the_excerpt();*/
?>
</div>
</a>
</article>
Here's the function
function wami_load_more_posts(){
if ( isset($_REQUEST) ) {
$paged = $_REQUEST['paged'];
$post_not_in = explode(',', $_REQUEST['post_not_in']);
$args = array(
'post_type' => 'post',
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => $post_not_in,
'post_status' => 'publish',
'paged' => $paged
);
if(isset($_REQUEST['category_in'])){
$args['category__in'] = $_REQUEST['category_in'];
}
$homeblog_query = new WP_Query($args);
if( $homeblog_query->have_posts() ):
while($homeblog_query->have_posts()):
$homeblog_query->the_post();
get_template_part('loop-post', 'actus');
endwhile;
endif;
wp_reset_postdata();
}
die();
}
add_action('wp_ajax_wami_load_more_posts', 'wami_load_more_posts');
add_action('wp_ajax_nopriv_wami_load_more_posts', 'wami_load_more_posts');

Categories