I used the plugin "Types" for wordpress.
I created a custom post called "slides" and created 2 custom fields "slide" and "phrase".
When I do :
$args=array(
'post_type' => 'slides',
'posts_per_page' => 5
);
$my_query = query_posts($args);
print_r($my_query);
I get the posts but I don't get any of the custom fields. Is there something I'm doing wrong? Thanks
Custom fields aren't returned as post objects. That is why you don't see them when doing a var_dump() of your query. You have to retrieve them manually unfortunately. You have to make use of get_post_meta to retrieve custom field data for a post
A point of note, never use query_posts. You should rather use WP_Query
WHY SHOULD query_posts NOT BE USED
Further info which you should read - query_posts() should be avoided?
Related
I have Yoast Seo plugin installed into my website and this brings back the number of social shares per post and stores it in the database.
Is it possible to change the archive page to order the posts by the number of social shares?
I've tried a few things listed here but can't seem to get it to work
<?php
/* The loop */
while ( have_posts() ) : the_post();
include( locate_template( 'content-' . $layout_this . '.php' ) );
endwhile;
?>
This one might be tougher than you think. Yoast SEO does not actually store the number of times any given social media outlet is actually shared. This is done by constantly running a check from the respective social media API and will retrieve the number of shares/likes which happened on that respective post.
Essentially what needs to be done is you'd need to hook onto the API a similar way Yoast is doing so, and then start comparing the values which are being sent back from the API in order to order your posts on the front-end by a number of most shares.
The idea for the functionality is pretty useful... I'm hoping that in the future there will be a new table added perhaps with the Yoast plugin which will actually save this data rather than having to refer to the API.
EDIT: In theory, this should be possible then if you know what meta value(s) you're working with from the database. As long as the posts have association with these values, this should be possible. You can query posts with specific meta keys and order them, too. Here's an example of what might work within your template, with some adjustments.
$args= query_posts(
array( 'post_type' => 'your post type', //for posts, enter 'post'
'order' => 'ASC',
'meta_key' => 'some_key', //the meta key
'orderby' => 'meta_value', //or 'meta_value_num'
)
);
See here as well, someone asked a very similar question : How to fetch posts order by meta value?
I have done some research on this topic and followed many tutorials but nothing seems to work, I was wondering if someone could help me out? I want to allow the search form in my Wordpress site to also include custom post types and custom meta fields. I would really appreciate it if someone could help me out. THANKS!
Archives.php only shows content of type 'post', but you can alter it to include custom post types. Add this filter to your functions.php file:
function namespace_add_custom_types( $query ) {
if ( $query->is_search )
$query->set( 'post_type', array( 'post', 'YOUR_CUSTOM_POST_HERE') );
return $query;
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
Wordpress search functionality looking search query only in 'posts' table in DB, but custom fields are saved on 'post_meta' table. So, firstly you need to LEFT JOIN these two tables, secondly change query to DB, and finally - prevent duplicates in searching. Please look at this link, here is the code you must paste into functions.php with an explanation -> https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
I'm developing a wordpress page and I'm looking for the best practices to send custom DB queries. I created a archive page with a list of artists. These names are stored in a custom post type's custom taxonomy. Now I want to code links on every name, which should lead the visitor to a page where all the posts that have this artists name in this custom field.
I know how to create the custom DB query, but how do I submit the name? Just over a normal POST-request? Is there a convenient way to do this within WP?
Thanks for your help.
Dan
you said in your question "custom post type's custom taxonomy"
you can create a custom taxonomy template to list all posts under a particular taxonomy. you can use this detailed tutorial how to use taxonomy.
http://code.tutsplus.com/tutorials/introducing-wordpress-3-custom-taxonomies--net-11658
I think the best way to do this would be store the artists name as a meta value attached to the post. Then you could use Wordpress inbuilt meta query to easily output all posts with the artists name stored as a meta value.
$meta_query_args = array(
array(
'key' => 'artist',
'value' => 'John Doe',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
I have searched questions similar to mine but with no luck finding the answer I need.
I have Authors and I have Custom Post Types (CPT). My search results already display all CPT's -- but, additionally, I need something more specific than that. I need my search function to allow combined queries for a specific Author and specific CPT. For example, all Blogs by Albert Einstein.
This url "/?s=%20&author_name=alberteinstein" returns all posts across CPT's by Albert Einstein.
But if I add "&post_type=blogs" for the full url to filter for the CPT like this:
"/?s=%20&author_name=alberteinstein&post_type=blogs"
it does not filter for just Blogs -- it still returns all CPT's by the Author, same as above.
I need to be able to query for an Author and specific CPT.
This has been driving me crazy for weeks. Any help would be greatly appreciated.
This may help (as worded on the WordPress Codex post types page). Basically, it may be that your custom post type (CPT) isn't registered for archive queries although it is legitimately registered for use as a CPT.
Registering a custom post type does not mean it gets added to the main query automatically. If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook.
// Show posts of 'post', 'page' and 'movie' post types on home page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'movie' ) );
return $query;
}
The WordPress codex has a lot of examples of how to register custom taxonomies with custom post types, but I couldn't find much about using built-in taxonomies (tags & categories) with cpts.
I have a cpt called listings, and I need to add the standard category and tag UI elements to the listing cpt page. I also need to do this with code in my functions.php, rather than using a plugin.
Not a problem at all. When you register the post type, just add this argument to the array:
'taxonomies' => array( 'category', 'post_tag' )
Suppose you defined your cpt (custom post type) by the following:
register_post_type('listings', $args); // where $args is an array of your cpt settings
Then you could use the following to add taxonomy:
// category-like:
register_taxonomy('listing_category', array('listings'), array('hierarchical' => true, ...));
// tag-like:
register_taxonomy('listing_tag', array('listings'), array('hierarchical' => false, ...);
In fact, I personally put those custom type definitions in my own plugin (not open for public as it provide my own site functionalities, which obviously not suit the others at all).
The problem of putting in functions.php increases the difficulty to change to a new theme (although changing theme is not so often, but for self-owned blog, it do happen in some day).
Moreover, the custom post types should be site-wide, not depending on the current theme. So semantically it should not be in the theme's directory.