i want to get the author's posts in spacific category and display it in author page
I tried this code and it didn't work, what is the problem? my code :
<?php
$url= get_site_url();
$author_id = get_the_author_meta('ID');
$args = array(
'author' => $author_id,
'tax_query' => array(
'relation' => 'AND',
array(
'cat' => '161,181,157',
'post_count' => '3',
),
),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$postid = get_the_ID();
echo '<a href='. $url .'?p='. $postid .'>' . get_the_title() . '</a>';
}
?>
Try the below code.
$url = get_site_url();
$author_id = get_the_author_meta('ID');
$args = array(
'author' => $author_id,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array(161,181,157)
),
),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$postid = get_the_ID();
echo '<a href='. $url .'?p='. $postid .'>' . get_the_title() . '</a>';
}
Related
Im using this php code on my page to show subcategories, and when click on certain subcategory should redirect to posts that have that subcategory:
<?php
wp_list_categories( array(
'taxonomy' => 'categorys',
'title_li' => '',
'child_of' => get_term_by( 'slug', 'osten', 'categorys' )->term_id,
) );
?>
I created taxonomy.php
I have 6 categories to filter these posts and each Category have its subcategories.
SO i need to filter posts, when i click on subcategory to show me only posts on it.
I used this code inside taxonomy.php but its showing me all of posts and not filtering by clicked subcategory:
<?php
$post_type = 'ortemp';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies($post_type); // names (default)
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
echo '<div id="activities_categories">';
foreach( $terms as $term ) :
$customposts = new WP_Query(
array(
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $term->slug,
'field' => 'slug',
// 'category' => 'category-1'
)
)
)
);
if( $customposts->have_posts() ):
while( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
endforeach;
echo '</div>';
endforeach;
?>
try these code to your 'taxonomy.php' file.
$currentTerm = get_queried_object();
$subTerms = get_term_children( $currentTerm->term_id, $currentTerm->taxonomy );
if ( ! empty( $subTerms ) ) {
echo '<div id="activities_categories">';
foreach ( $subTerms as $subTerm ) :
$customposts = new WP_Query(
array(
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => $currentTerm->taxonomy,
'terms' => $subTerm,
'field' => 'term_id',
)
)
)
);
if ( $customposts->have_posts() ):
while ( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
endforeach;
echo '</div>';
}
another example
$currentTerm = get_queried_object();
$subTerms = get_term_children( $currentTerm->term_id, $currentTerm->taxonomy );
if ( ! empty( $subTerms ) ) {
echo '<div id="activities_categories">';
$customposts = new WP_Query(
array(
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => $currentTerm->taxonomy,
'field' => 'term_id',
'terms' => $subTerms,
)
)
)
);
if ( $customposts->have_posts() ):
while ( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
echo '</div>';
}
I have a product category called Cable, and I want to grab all posts assigned to this category and output the title of said posts. This category is from the custom taxonomy 'Category' that woo-commerce adds, I'm not sure if that makes things harder? but I haven't found a solution yet.
Could anyone assist with this?
Try this code:
And modify as per your requirements:
add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {
$query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( '18' ), //Your custom category id
),
),
)
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
}
Use this code to show all posts assigned to the category cable
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'cable' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post);
?>
<div>
<?php
the_title();
the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have created with list
add_shortcode( 'pi_cable_posts', 'pi_posts' );
function pi_posts() {
$pi_query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1, //--1 for unlimited and number to limit
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 'YOUR ID HERE' ), //ADD YOUR ID HERE
),
),
));
if ( $pi_query ->have_posts() ) {
echo '<ul>';
while ( $pi_query ->have_posts() ) {
$pi_query ->the_post();
echo '<li><h2><a href="'.get_the_permalink().'">' . get_the_title() . '</h2></li>';
}
echo '</ul>';
}
wp_reset_postdata();}
You can use WP_Query() with tax_query().
EDIT:
Try out this code to get products on category page.
add_shortcode('product_list', 'zillion_show_products_title_by_cat', 10);
function zillion_show_products_title_by_cat()
{
if(!is_product_category()){
return;
}
$term = get_queried_object();
ob_start();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term->term_id, // 21 is category id When you have more term_id's seperate them by comma.
'operator' => 'IN'
)
);
$the_query = new WP_Query($args);
// The Loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<h3>' . get_the_title() . '</h3>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_clean();
return $contents;
}
So I have a custom type called ("knowledge_base") which has a taxonomy called ('section') and one of them is ('dog bite'). Right now I am on example.com/section/dog-bite/ and I am trying to show the posts located here. This is what I have so far so I am not sure what is missing but it just displays ALL the posts from all sections.
$current = get_queried_object();
$args = array(
'post_type' => 'knowledge_base',
'tax_query' => array(
array(
'taxonomy' => 'section',
'field' => $current->slug,
'terms' => $current->name
)
)
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
There is supposed to be only 2 posts
Check this code.
$args = array(
'post_type' => 'knowledge_base',
'tax_query' => array(
array(
'taxonomy' => 'section',
'field' => 'slug', // ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’
'terms' => $current->slug, // It's will be $term->slug
)
)
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
I'm trying to create a custom easy digital downloads (EDD) shortcode and I can't make the atts working. Meaning I would like to be able to change the category and tag names while using the shortcode, e.g :
<?php echo do_shortcode( '[my_shortcode taxonomy="download_tag" field="name" terms="Premium"]' ); ?>
But all I get is the default term ( Free ).
The code:
function my_shortcode_function($product_args, $content = null) {
global $post;
$current_page = get_query_var('paged');
$offset = $current_page > 0 ? $per_page * ($current_page-1) : 0;
$product_args = array(
'post_type' => 'download',
'posts_per_page' => '6',
'tax_query' => array(
array(
'taxonomy' => 'download_tag',
'field' => 'name',
'terms' => 'Free'
),
),
'offset' => $offset
);
$products = new WP_Query($product_args);
if ($products->have_posts()) : $i = 1;
while ($products->have_posts()) : $products->the_post();
$tags = get_the_term_list( get_the_ID(), 'download_tag' );// get tags link
$tags = strip_tags( $tags ); // get rid of the tag link
$category = get_the_term_list( get_the_ID(), 'download_category' ); // get category link
echo '<div class="col-md-4 col-lg-4">';
get_template_part( 'template-parts/content-download-card-post' );
echo '</div>';
$i+=1;
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
}
add_shortcode( 'my_shortcode', 'my_shortcode_function' );
I'm sure there are better ways to do it but I'm still new in php and the EDD docs lacks of useful examples for newbies like me. So I'll appreciate any help. Thanks a lot.
It's funny that I implemented this from a woocommerce answer and it works like a charm. Now I can change the tag, category, or the number of posts. If anyone needs it here it is:
// Creating a shortcode that displays a random product image/thumbail
if( !function_exists('prod_listing_params') ) {
function prod_listing_params( $atts ) {
ob_start();
$atts = shortcode_atts( array (
'type' => 'download',
'order' => 'date',
'orderby' => 'title',
'posts' => 6,
'category' => '', // category name
'tag' => '', // tag name
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'posts_per_page' => $atts['posts'],
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'download_category',
'field' => 'name',
'terms' => $atts['category'],
),
array(
'taxonomy' => 'download_tag',
'field' => 'name',
'terms' => $atts['tag'],
)
),
) );
if ( $query->have_posts() ) { $i = 1;
?>
<?php while ( $query->have_posts() ) : $query->the_post();
$tags = get_the_term_list( get_the_ID(), 'download_tag' );// get tags link
$tags = strip_tags( $tags ); // get rid of the tag link
$category = get_the_term_list( get_the_ID(), 'download_category' ); // get category link
?>
<?php echo '<div class="col-md-4 col-lg-4">';
get_template_part( 'template-parts/content-download-card-post' );
echo '</div>'; $i+=1; ?>
<?php endwhile;
wp_reset_postdata(); ?>
<?php
$myvar = ob_get_clean();
return $myvar;
}
}
add_shortcode( 'list_products', 'prod_listing_params' );
And the use:
<?php echo do_shortcode( '[list_products tag="Free" posts="3"]' ); ?>
I have created / mashed together a cool search through a specified category for my blog. Using Ajax to load the results without the reload.
When I search - no matter the term I search. I receive all posts.
I use ACF for the content & the author. I also reference products using the field featured_product_title. These fields are used within my page like this:
<?php if ( have_rows('case_study_page_content') ): ?>
<?php
while (have_rows('case_study_page_content')): the_row();
$title = get_sub_field('title');
$author = get_sub_field('author');
$content = get_sub_field('content');
?>
<div class="">
<h1 class=""><?php echo $title; ?></h3>
<h3 class=""><?php echo $author; ?></h4>
<p><?php echo $content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php
while (have_rows('featured_products')): the_row();
$featured_product_title = get_sub_field('featured_product_title', 'featured_products');
?>
With these in mind my current search looks like this (functions.php):
// CASE STUDY SEARCH
function my_search(){
$args = array(
'orderby' => 'date',
'order' => $_POST['date']
);
if( isset( $_POST['s'] ) ):
/*
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
's' => $_POST['s']
);
*/
if( have_rows('case_study_page_content') ):
while( have_rows('case_study_page_content') ) : the_row();
$title = get_sub_field('title');
$author = get_sub_field('author');
$content = get_sub_field('content');
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $title,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => $author,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => $content,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
)
)
);
endwhile;
endif;
$query = new WP_Query($args);
if( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
echo "<article class=\"post-box " . get_post_class() . "\">";
echo "";
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
echo "<img src=\"" . $url . "\" />";
echo "<h2>" . get_the_title() . "</h2>";
$case_study = get_field('case_study_page_content');
if( $case_study ):
while( have_rows('case_study_page_content') ): the_row();
$case_study_author = get_sub_field('author');
echo "<p>" . $case_study_author . "</p>";
endwhile;
endif;
echo "</article>";
endwhile;
wp_reset_postdata();
else :
echo 'No case studies found';
endif;
die();
endif;
}
add_action('wp_ajax_customsearch', 'my_search');
add_action('wp_ajax_nopriv_customsearch', 'my_search');
I guess my question is how do I add ACF's into the $args array...?
Please can someone help me successfully compare the 'key' to the 'value' in my WP_Query($args)?
Thanks everyone, Jason.
test this but without conviction
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'case_study_page_content_title',
'compare' => 'like',
'value' => '%'.$_POST['s'].'',
),
array(
'key' => 'case_study_page_content_author',
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => 'case_study_page_content_content',
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
)
)
);
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
$result = $query->query_vars['s'];
$query->query_vars['s'] = '';
$query->set('meta_query', array('relation' => 'OR',
array(
'key' => 'acf_name', // ACF FIELD NAME OR POST META
'value' => $result,
'compare' => 'LIKE',
)
));
$query->set('post_type', 'post'); // optional POST TYPE
}
}
add_filter( 'pre_get_posts', 'custom_search_query');