Need some help.
I have code in functions.php. when going to myweb.com/random=1 it goes to random post but I want it picks random only from chosen categories. I tried that but it still picks it from all categories.
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('category=14,17,23,28,32&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
Try seeing if this revised one works
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$arguments = array('orderby' => 'rand', 'numberposts' => 1, 'category' => '14,17,23,28,32');
$posts = get_posts( $arguments );
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
If you query some custom posts, you'll need to do a taxonomy query instead of using category parameter - which refer to the default post type categories.
$args = array(
'numberposts' => 1,
'orderby'=> 'rand',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array(14,17,23,28,32)
)
)
);
$posts = get_post($args);
Related
we have made a custom api to get all the post in descending order and we want to add pagination in that api, I have read other questions and answers also but didnt get any idea so can some one explain me with a simple code with pagination so that I can understand how it works.
This is my code so how can I add pagination can anyone explain to me because I searched other questaion also but I didnt get any idea.
define('API_ENDPOINT_VERSION',1);
//flush the rewrite rules on plugin activation
function apiendpoint_activate()
{
flush_rewrite_rules();
}
register_activation_hook(__FILE__,'apiendpoint_activate');
function apiendpoint_register_endpoints(){
register_rest_route(
'api/v1',
'/post',
[
'methods' => 'GET',
'callback' =>'api_get_post',
]
);
}
add_action('rest_api_init','apiendpoint_register_endpoints');
function api_get_post($request){
$ar = array( 'post_type'=>'post',
'posts_per_page'=>15,
'orderby' => 'date',
'order' => 'DESC',
);
$posts = get_posts($ar);
//var_dump($posts);
//exit;
$a = array();
if($posts){
foreach ($posts as $post) {
$a[]= array(
'title'=>$post->post_title,
'link'=>get_the_permalink($post->ID),
'category'=>get_the_category($post->ID),
'published_date'=>get_the_date('l, F j, Y',$post->ID),
'guid'=>$post->guid,
'image'=>get_the_post_thumbnail_url($post->ID,'large'),
'description'=>$post->post_excerpt,
'source'=>"Nepaljapan"
//'img'=>$img
);
}
return $a;
}
}
Try the below code:
define('API_ENDPOINT_VERSION', 1);
//flush the rewrite rules on plugin activation
function apiendpoint_activate() {
flush_rewrite_rules(); } register_activation_hook(__FILE__, 'apiendpoint_activate');
function apiendpoint_register_endpoints() {
register_rest_route(
'api/v1',
'/post',
[
'methods' => 'GET',
'callback' => 'api_get_post',
]
);
} add_action('rest_api_init', 'apiendpoint_register_endpoints');
function api_get_post($request) {
$ar = array('post_type' => 'posts',
'posts_per_page' => 15,
'orderby' => 'date',
'order' => 'DESC',
'paged' => ($_REQUEST['paged'] ? $_REQUEST['paged'] : 1)
);
$posts = get_posts($ar); //var_dump($posts); //exit; $a = array();
if ($posts) {
foreach($posts as $post) {
$a[] = array(
'title' => $post -> post_title,
'link' => get_the_permalink($post -> ID),
'category' => get_the_category($post -> ID),
'published_date' => get_the_date('l, F j, Y', $post -> ID),
'guid' => $post -> guid,
'image' => get_the_post_thumbnail_url($post -> ID, 'large'),
'description' => $post -> post_excerpt,
'source' => "Nepaljapan"
//'img'=>$img
);
}
return $a; }
}
Call API call as below:
/wp-json/api/v1/post?paged=1
Increase the paged value by 1 to get next paging posts.
Hope this helps!
After saving/updating a custom wordpress posttype i'm calling a script:
function update_save_post( $post_id, $post, $update ){
if ($post->post_type == 'my_custom_posttype')
{
include 'api/index.php';
}
}
add_action( 'save_post', 'update_save_post', 10, 3);
In the index.php i retrieve all the posts of the my_custom_posttype and loop throught them. Inside the loop 'get_permalink' and 'get_the_category' return empty (empty string and empty array). The $post and $acf arrays contain the fields as expected. Permalink and get_the_category work normal when used inside the archive page.
$posts = get_posts([
'post_type' => 'my_custom_posttype',
'post_status' => 'publish',
'numberposts' => -1,
'order' => 'ASC'
]);
foreach ($posts as $post) {
print_r($post);
echo get_permalink($post->ID); //empty string!?
$acf = get_fields($post->ID);
print_r($acf);
$cats = get_the_category($post->ID);
print_r($cats); //empty array!?
}
Any ideas?
Remove $post->ID and add only $post and it will work as you are using foreach
$posts = get_posts(array(
'post_type' => 'my_custom_posttype',
'post_status' => 'publish',
'numberposts' => -1,
'order' => 'ASC'
));
foreach ($posts as $post) {
print_r($post);
echo get_permalink($post->ID); //empty string!?
$acf = get_fields($post->ID);
print_r($acf);
$cats = get_the_category($post->ID);
print_r($cats); //empty array!?
}
I want my acf field "unique_field" in my custom post type "places" to be unique.
I used acf/validate_value documented here -> https://www.advancedcustomfields.com/resources/acf-validate_value/
https://support.advancedcustomfields.com/forums/topic/solved-check-if-value-already-exist-2/
I tried to put this on my functions.php but it doesn't work.
add_filter('acf/validate_value/name=unique_field', 'validate_unique_field_filter', 10, 4);
function validate_unique_field_filter($valid, $value, $field, $input) {
if (!$valid || $value == '') {
return $valid;
}
// query posts for the same value
global $post;
$args = array(
'post_type' => 'places',
'post__not_in' => array($post->ID),
'meta_query' => array(
array(
'key' => 'unique_field',
'value' => $value
)
)
);
$query = new WP_Query($args);
if (count($query->posts)) {
$valid = 'Place already exist';
}
return $valid;
}
Are there any mistakes? Please Advise. Thank you!
it is missing 'meta_key' => 'unique_field' in the meta_query array
the filter 'acf/validate_value/name=unique_field' should be 'acf/validate_value/key=field_unique_field'
I actually have a hook like this on pre_get_post:
add_action('pre_get_posts', 'my_post_pre_get_posts');
function my_post_pre_get_posts($query)
{
if (!is_admin() && $query->is_main_query() && $query->is_home()) {
$query->set('posts_per_page', 3);
//FILTERS
$type = array();
if(!empty($_GET['type'])) {
$type = explode(',', $_GET['type']);
}else {
$type = array('post', 'testimonial', 'project');
}
$query->set('post_type', $type);
//CATEGORIES
if(!empty($_GET['category'])) {
$category = explode(',', $_GET['category']);
$args = array(
'relation' => 'OR',
);
foreach ($category as $categorie) {
array_push($args,
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $category
)
);
}
$query->set('tax_query', $args);
}
}
}
With this, my home display posts and also two other custom post type: "project" and "testimonials".
My posts have categories (but project and testimonials haven't it) and I would like to filters them with it. But I don't want that when I filter with the taxonomy its hide the project and testimonials results.
So instead of have an "AND" relation between post_type and taxonomy I want an "OR" relation. Do you think it's possible ? Do I have to rewrite the global $wpdb ?
set 'relation' after your category loop alongside your 'tax_query' set
$query->tax_query->relation ='OR';
I was trying to create shortcode for custom taxonomy terms dynamically. But failing to do so.
Suppose, if there is a term called "wordpress" then I should be able to query all the posts associated with that term via shortcode.
To be more precise, suppose if there is a taxonomy called 'event' and under that taxonomy there are multiple terms. So, I was trying to query posts under each of the term via shortcode of each of the term.
Here is what I tried:
function wordpress_recent_post( $atts, $content ) {
$a = shortcode_atts( array(
'cat' => '',
), $atts );
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'category_name' => $a['cat'],
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
);
$recent_posts = new WP_Query( $args );
ob_start();
if ( ! $recent_posts-> have_posts() ) {
return 'No Posts Found for ' . $a['cat'];
}
while ( $recent_posts->have_posts() ) {
$recent_posts->the_post();
the_title( '<h2>', '</h2>' );
if ( '' != $a['cat'] ) {
$href = '/category/' . $a['cat'];
} else {
$href = '/blog';
}
echo "<p><a href='$href'>Read More" . ucwords( $a['cat'] ) . '</a></p>';
}
wp_reset_query();
return ob_get_clean();
}
add_shortcode( 'wordpress_recent_post', array($this, 'wordpress_recent_post') );
And then I used this to call the posts from a term called "features" whose id is '183' (suppose)
[wordpress_recent_post cat="183"]
Any help would be really very appreciable.
Thanks!
Adding term slug did it. There should be slug not id, like this:
[wordpress_recent_post cat="features"]