I am using the following code to get a yearly archive of my posts with a post type of 'foi'.
Here is my code:
wp_get_archives(
array(
'post_type' => 'foi',
'type' => 'yearly',
'limit' => '10',
'show_post_count' => 'true'
)
);
I have a taxonomy set-up for the post type foi and I would like to use that in the wp_get_archives() function somehow. An example would be to show the yearly archive for all posts with a post type of foi but also with a taxonomy of document.
How can this be achieved?
You get it using filter like below:
In templates/custom-archive-template.php
add_filter( 'getarchives_where', 'custom_archive_by_category_where' );
add_filter( 'getarchives_join', 'custom_archive_by_category_join' );
$args = array();
wp_get_archives(
array(
'type' => 'yearly',
'format' => 'option',
'post_type' => 'news',
)
);
remove_filter( 'getarchives_where', 'custom_archive_by_category_where' );
remove_filter( 'getarchives_join', 'custom_archive_by_category_join' );
In functions.php:
function custom_archive_by_category_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}
function custom_archive_by_category_where($x) {
global $wpdb;
$current_term_slug = get_query_var( 'news_category' );
if (!empty($current_term_slug)) {
$current_term = get_term_by('slug', $current_term_slug, 'news_category');
if (is_wp_error($current_term) ) {
return $x;
}
$current_term_id = $current_term->term_id;
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'news_category' AND $wpdb->term_taxonomy.term_id IN ($current_term_id)";
}
return $x;
}
Please change news_category to your desire category slug.
Originally posted on: https://developer.wordpress.org/reference/functions/wp_get_archives/#div-comment-3182
Related
I have multiple products, whose sku names are like AL-888, A-2323, AL-etrere.
I want to find products on basis of this sku name's first two words.
I am creating shortcode for this purpose, but can't able to get it work.
Any help should be appreciated.
Code for this shortcode:
function all_state_list_function(){
$name = $_GET['sku']; //this give sku value from url like Al:2323
echo $name;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'free_form',
'value' => '1',
),
array(
'key' => '_sku',
'value' => $name.'-%',
'compare' => 'LIKE',
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product_id = get_the_ID();
echo $product_id;
endwhile;
wp_reset_query();
}
add_shortcode( 'all_state_list_shortcode', 'all_state_list_function' );
Based on your first version code that had an SQL query, here is the correct way to get product IDs from first SKU characters value using GET method from an URL:
add_shortcode( 'all_state_list_shortcode', 'all_state_list_function' );
function all_state_list_function(){
if ( isset($_GET['sku']) && ! empty($_GET['sku']) ) {
ob_start();
$sku = esc_attr( $_GET['sku'] ) .'%';
global $wpdb;
$results = $wpdb->get_col( "
SELECT p.ID FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'product' AND p.post_status LIKE 'publish'
AND meta_key LIKE '_sku' AND meta_value LIKE '$sku'
" );
if( count($results) > 0 ) {
echo implode(',', $results);
} else {
echo 'Nothing found';
}
return ob_get_clean();
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I have registered taxonomies for multiple post types thinking this was the best way to set the site up rather than have the same taxonomies duplicated.
However, now I have run into an issue where I need to list the used taxonomies for a post type but its listing taxonomies for all both types. How can I resolve this issue? Niether get_categories or get_terms seem to have an option to specify which post type you want to get taxonomies for.
EDIT
NOTE: Each post type also has multiple taxonomies
Can anyone help?
register_taxonomy(
'sectors',
array('case-study', 'resource'), //used in multiple post types
[
'labels' => [
'name' => __( 'Sectors' ),
'singular_name' => __( 'Sector' ),
],
'hierarchical' => true,
'show_admin_column' => true,
]
);
$sectors = get_categories( array('taxonomy' => 'sectors') ); //prints out selected taxonomies for both case studies and resources when I want just resources.
$services = get_categories( array('taxonomy' => 'services') );
Try this
<?php
$custom_terms = get_terms('custom_taxonomy_name');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type_name',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_name',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
?>
I found the following code which works like a charm :)
function df_terms_clauses( $clauses, $taxonomy, $args ) {
if ( isset( $args['post_type'] ) && ! empty( $args['post_type'] ) && $args['fields'] !== 'count' ) {
global $wpdb;
$post_types = array();
if ( is_array( $args['post_type'] ) ) {
foreach ( $args['post_type'] as $cpt ) {
$post_types[] = "'" . $cpt . "'";
}
} else {
$post_types[] = "'" . $args['post_type'] . "'";
}
if ( ! empty( $post_types ) ) {
$clauses['fields'] = 'DISTINCT ' . str_replace( 'tt.*', 'tt.term_taxonomy_id, tt.taxonomy, tt.description, tt.parent', $clauses['fields'] ) . ', COUNT(p.post_type) AS count';
$clauses['join'] .= ' LEFT JOIN ' . $wpdb->term_relationships . ' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN ' . $wpdb->posts . ' AS p ON p.ID = r.object_id';
$clauses['where'] .= ' AND (p.post_type IN (' . implode( ',', $post_types ) . ') OR p.post_type IS NULL)';
$clauses['orderby'] = 'GROUP BY t.term_id ' . $clauses['orderby'];
//print_r( $clauses );
}
}
return $clauses;
}
add_filter( 'terms_clauses', 'df_terms_clauses', 10, 3 );
$terms = get_terms( 'animal_cat', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'animal',
'animal_cat' => $term->slug
);
$query = new WP_Query( $args );
}
Trying to write a piece of code to expand a theme that I am using in Wordpress.
Basically, I want to get all custom post types and put it into an array for a select - the issue I am having is that I need to add the option values in the array and I cannot put a foreach loop in the array so not sure how to do this.
In the code below you will see the code:
'options' => array(),
This is where the custom posts need to be in the format:
'PostID' => esc_html__( 'Post Name', 'builder' ),
Here is my code:
function get_fields() {
$fields = array(
'get_post_names' => array(
'label' => esc_html__( 'Url Opens', 'builder' ),
'type' => 'select',
'option_category' => 'configuration',
'options' => array(),
'toggle_slug' => 'post_names',
'description' => esc_html__( 'Here you can choose whether or not your link opens in a new window', 'et_builder' ),
),
);
global $wpdb;
$custom_post_type = 'custom_post_name';
$results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish'", $custom_post_type ), ARRAY_A );
if ( ! $results )
return;
foreach( $results as $index => $post ) {
$fields['options'][] = array (
$post['ID'] => esc_html__( $post['post_title'], 'builder' ),
);
}
return $fields;
}
Any help would be much appreciated.
Thanks
Hopefully this may work
function generate_post_select($select_id, $post_type, $selected = 0) {
$post_type_object = get_post_type_object($post_type);
$label = $post_type_object->label;
$posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
foreach ($posts as $post) {
echo $post->post_title;
}
}
$select_id is used as the name and id of the select, $post_type is the type you want to be made into the select and $selected is the post id you want selected in the select box.
Found a solution if anyone wants to know.
Changed the 'option' to be the following and removed the code from global $wpdb; down.
'options' => array_reduce( get_posts( 'post_type=custom_post&posts_per_page=-1' ), function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
}),
My custom wp search query in
$args = array(
'post_type' => 'post',
'taxonomy' => 'location',
'field' => 'name',
'posts_per_page' => 10,
'term' => $keyword,
);
$wp_query = new WP_Query($args);
I have 2 post which location is London and oxford,london .Now when i search london then it show only 1 post**(1st one)** .Also when i search oxford then it show no result found.
When i search oxford london then its working fine.
Befor WP_Query Run write this code
function advance_search_where($where){
global $wpdb;
if (is_search())
$where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')";
return $where;
}
function advance_search_join($join){
global $wpdb;
if (is_search())
$join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
return $join;
}
function advance_search_groupby($groupby){
global $wpdb;
// we need to group on post ID
$groupby_id = "{$wpdb->posts}.ID";
if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby;
// groupby was empty, use ours
if(!strlen(trim($groupby))) return $groupby_id;
// wasn't empty, append ours
return $groupby.", ".$groupby_id;
}
add_filter('posts_where','advance_search_where');
add_filter('posts_join', 'advance_search_join');
add_filter('posts_groupby', 'advance_search_groupby');
Now Run Your WP_Query Like this
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
's' => $keyword,
);
$wp_query = new WP_Query($args);
After all your result and that you want you have to just remove the above filter coz if you not remove that then it will apply in other wp_query of that page.
remove_filter('posts_where','advance_search_where');
remove_filter('posts_join', 'advance_search_join');
remove_filter('posts_groupby', 'advance_search_groupby');
I'm trying to filter my posts to only show the ones with that have a custom value for the field "Model" while sorting the posts by another custom field called "Price."
Here's the function I'm using (not working):
<?php
global $query_string;
query_posts( $query_string . "&meta_value=Model&orderby=meta_value&meta_key=Price&order=ASC");
?>
This function only shows Models, yet doesn't sort the posts by Price. If i add &meta_value=Model after order=ASC it sorts by Price but shows all posts, and not just Models.
Have you looked at http://codex.wordpress.org/Class_Reference/WP_Query
Specifically this section:
Multiple Custom Field Handling:
Display posts from several custom field:
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
did you try and array?
$args = array(
'meta_value' => array('Model','Price')
);
query_posts($args);
I know this is an old question, but I needed the answer today and couldn't find it anywhere. I found the question and then created an answer (shown below):
<?php
$sql = "
SELECT ID, meta1.meta_value, meta2.meta_value from $wpdb->posts p
JOIN $wpdb->postmeta meta1 ON meta1.post_id = p.ID
JOIN $wpdb->postmeta meta2 ON meta2.post_id = p.ID
WHERE p.post_status = 'publish'
AND p.post_type = 'post'
AND meta1.meta_key = 'Model'
AND meta2.meta_key = 'Price'
ORDER BY meta2.meta_value DESC
";
$posts_with_meta = $wpdb->get_col($sql);
$my_query = new WP_Query();
foreach ($posts_with_meta as $p) {
$post = get_post(intval($p));
setup_postdata($post);
print_r($post);
}
?>