how to show posts data on count hyperlink in wordpress - wordpress

In wordpress, I am showing a count# of posts based on various custom taxonomies. I have retrieved this count using wp-query->found_posts used in functions.php file (used tax-query). I want to have this count number as hyperlink to show details of the posts. Any suggestion on how to get this done?
I first tried to pass the WP_Query object to the php which contains the post data but obj->found_posts did not work in the php file.

You will need to capture posts' IDs from within the WP_Query you're running.
Example:
$post_ids = array();
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$post_ids [] = get_the_ID() ;
endwhile;
Later on, you can use these IDs however you want.

Related

Trying to access ACF value in functions.php, in woocommerce_email_order_details action

I have a Wordpress installation with Woocommerce and I added a ACF field to the orders. It's called 'booking_reference'.
Inside my functions.php file, on checkout I update the 'booking_reference' custom field. It gets saved to the database. All is working as it should.
In my next function i'm trying to email this custom field to the customer. I use this code:
$order_id = $order->get_id();
$reference_number_acf_order = get_field('booking_reference', $order_id );
When I echo $reference_number_acf_order, it's empty. The $order_id variable is correct. It contains the order correct order ID.
The rest of the email to the customer is correct, it contains data from the order. Only the ACF value is empty.
Are u using WP_Query() or something like in your new function?
If so. try calling wp_reset_query(); first:
wp_reset_query();
$reference_number_acf_order = get_field('booking_reference', $order_id );

How to access WordPress post admin data inside the loop?

I'm trying to access some fields that have been assigned to a post via a plugin named Snax.
You can see here in the admin that "Story" has being assigned to a post as a "Snax Format":
But when I check the $post object inside the loop on the front end using something like $my_post = get_post(); var_dump($my_post); I can't see any anything relating to snax or "story".
What php can I use to access that post data on the front end inside the loop?
I'm asking because I want to use that field data inside my post template.
I think the plugin will store they data as meta_values. You can retrieve this data with get_post_meta()
$meta_data = get_post_meta( get_the_ID() );
var_dump( $meta_data );

How to retrieve the latest categories assigned to a post currently under editing?

I need to limit the post tile length of a post belonging to a specific category while editing. So I need to check what categories have been assigned to the post under editing and decide whether to limit or not its post title.
I use "wp_insert_post_data" to do the job
add_filter( 'wp_insert_post_data' , 'limit_title_length' , '99', 2 );
But what I found is that the categories returned from passed $postarr are existing categories. Not the latest categories. So it would not work for new post or if categories being changed while editing.
$post_category = $postarr['post_category'];
I also checked get_the_category() inside the function, and that also returns existing categories, not the latest categories if category assignment changed.
My codes so far...
function limit_title_length( $data, $postarr ) {
// set up variables like max length, category id to limit
...
// get post id, title and categories from $data and $postarr passed
$title = $data['post_title'];
$id = $postarr['ID'];
$post_category = $postarr['post_category'];
// check if the specified category exists in the categories assigned to this post
...
// process $title, reset $post_title in $data
...
$data['post_title'] = $title;
return $data;
}
add_filter( 'wp_insert_post_data' , 'limit_title_length' , '99', 2 );
wp_insert_post_data fires in the very late stage of post publishing. I expected to get the latest categories from $postarr['post_category'];
but it's not in my case here. Any solutions or alternatives?
So just to be clear - You want to identify the most recent category added to a post?
If this is the case you will be hard pressed to do so as Wordpress does not save meta for added categories. If you are skilled enough you could script such a function and save the data in a custom table. Then retrieve it for use.
If you know the name of the category you are looking for you can use has_category! .

ACF Post object not returning data

I need help in Advanced custom field pro plugin
i want to display 3 post of credit taxonomy to the home page
i had edited my field group like :
Field Label : home credit
Field Name : home_credit
Field Type : Post object
Filter by taxonomy : credit
Now, when i print the data to the homepage by writing this :
- get_field('home_credit', get_the_ID()) );
i am not getting the value from the post object or how to retrieve its value please help. I had also try to debug the value by print_r but no value returning.
if you need fetch data on taxonomy page by ACF,
<?php the_field('home_credit','credit_' . term_id); ?>
Looks like the ID you are using to retrieve the data is not correct. You might have to check what "get_the_ID()" returns to make sure you are using the correct page ID. One solution would be to use "get_queried_object_id()" instead. Try :
$home_credit = get_field( 'home_credit', get_queried_object_id() );
Basically get_queried_object_id() will get the ID of the object queried in the current wp_query. get_the_ID() might fail if post data is not set.

wordpress page id in friendly url

How to get the page id in wordpress in the case of friendly url?
Inside the Loop: http://codex.wordpress.org/Function_Reference/the_ID
Outside the Loop: global $wp_query; $id = $wp_query->post->ID;
I'm guessing you're talking about doing it via the Admin interface. Can't you just add it into the custom format textbox using the %post_id% string:
/archives/%post_id%
If you are within the Loop (most of the time meaning within the while(have_posts()) you should be able to use: global $post; $id = $post->ID; to get the ID

Resources