How do I retrieve metadata from the Wordpress Media Gallery? - wordpress

In the Wordpress Media Library, when you add a piece of media, you're invited to also add other 'metadata': Title, Caption, Alternate Text, and Description.
Using get_the_title() will return the title, but trying get_the_caption() simply breaks the page.
How do I get this data, or metadata on the page. I've also tried using wp_get_attachment_metadata(), which returns an array. Does anyone know what values are in this array? I know it has height and width.

I see that this metadata is stored to the wp_posts table. For example, the caption for a photo is in column post_excerpt. Since the title column is post_title, you might want to try the same naming convention and try a call to get_the_excerpt(). Here are the other names of the columns in the wp_posts table
post_author post_date post_date_gmt post_content post_title post_excerpt post_status comment_status ping_status post_password post_name to_ping pinged post_modified post_modified_gmt post_content_filtered post_parent guid menu_order post_type post_mime_type comment_count
Hope this helps

Related

How to change product category slug in bulk in Woocommerce?

I am working on a Woocoomerce site with many categories.
I have to add some text at the end of the categories (suffix) slug. Like "klm".
How to change product category slug in bulk in Woocommerce?
For example,
Furniture > Kitchen Furniture
The permalink for the category is as follows:
"kitchen-furniture"
What I want to do is add a new text (suffix) to the end of all (main category or subcategories).
For example,
"kitchen-furniture-klm"
Yes, this can be done from wp-admin>Products>Categories. However, as I said at the beginning, there are many categories.
There are 22 pages of entries for categories on Stackoverflow. I've looked at almost all of them. However, I couldn't find what I was looking for. I would appreciate if someone could guide me where to start.
Note: Please take database backup before execute following update query
You can do this in database, using following query
UPDATE wp_terms SET slug = CONCAT(slug, '-klm') WHERE term_id IN(SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = 'product_cat')

How to change specific primary category name?

I have more than one category name in the post and I want to change specific primary category name to be normal category and then normal category to be primary category.
Let's say I have sponsored category and fashion category. So, I want to switch fashion category to be primary category because I do not want to show /sponsored/ in the URL.
Take Note: I have thousand of posts and I cannot switch the category name manually.
Using SQL and PHP you can run a foreach loop with all posts id that need to be updated.
A good starting point is an Excel file with all posts and its category ids that will be set as primary, so convert the spreadsheet to a json file to use in the PHP loop.
Below is the SQL query to do this:
UPDATE wp_postmeta SET meta_value = $new_primary_id WHERE post_id = $post_id AND meta_key = '_yoast_wpseo_primary_category';
Where $new_primary_id is the id of the category that will be set as primary, and $post_id
is the post id that will be updated.
If you using Yoast SEO Plugin there is the solution for you check here.
https://wordpress.org/support/topic/bulk-change-primary-category/

Wordpress: using query_posts with custom sort

I'm using
<?php query_posts('cat=34546&posts_per_page=5&order=DESC');
in order to get five posts from a certain category. The order is determined by the ID of the post. I would like it to be determined by another field, like post_date column in posts (let's say for the sake of this question that I manually change post_date that in the database). How can this be done?
Using the Query Posts orderby parameters.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
For example:
<?php query_posts('cat=34546&posts_per_page=5&order=DESC&orderby=date');
The reference I linked above has what values you can use for orderby.

Recover Custom Post Type Data

I have created CPT using CPT UI plugin and suddenly I lost all the important data. I don't have any backup and I need it backup. Is there any I can get it backup? I have SQL backup of last week.
Moreover I am seeing Count on CPT categories but they are showing Empty on main CPT page. for example CPT:
Animals --> showing (0) posts
Animal Categories is showing 100 counts that means It has data but something has hide it. I remember I updated Permalinks but I don't remember the previous settings
use this sqlQuery in your SQL Database (phpMyadmin or ...) and you will get a list of your custom post type posts with their info:
SELECT *
FROM {table prefix}_posts
WHERE post_type = 'your_custom_post_type_name'
AND post_status = 'publish'
your {table prefix} can be wp or some thing else if you've change it on wp setup step you can check it in your Database or wp-config.php file in your wp root.
you can replace * in query with whatever field name you want for example:
post_content, post_title, post_excerpt
to only get fields that you need.

How search by title in Wordpress?

please can you help me how do action which search posts only by title and sort by title.
I dont want change query search in include file query.php.
function sort_searchresult_by_title($k){
if(is_search()){
$k->query_vars['orderby'] = 'title';
$k->query_vars['order'] = 'ASC';
// some code for change search logic
}
}
add_action('pre_get_posts','sort_searchresult_by_title');
Thank you,
P. V.
By default, WordPress constructs the search query to look in the post_title and post_content fields. You can change it by hooking into the posts_search filter, and creating a new SQL clause there.
Did you know that there is a WordPress Stack Exchange, specific to all WordPress-related questions?

Resources