how to show the data of CPT UI plugin on website? - wordpress

I have used CPT UI to add some posts with taxonomies. I have filled two post data in CPT UI for practice. Now I want to show these post on a page. What all code I have to write.

You can use Wp_Query along with the post name that you created using CPT Ui plugin to display those posts. Like, e.g. i had created a post named as school then code to display all posts of School type is as following :
$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
$query->the_post();
echo $query->ID; // it will print the ID of post
endwhile;
Hope this will clear the things..

In order to pull in the custom fields/post meta, you will need to write some code within the WordPress loop (https://codex.wordpress.org/The_Loop) in your template file.
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
eg.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
For all post meta:
$meta = get_post_meta( get_the_ID() );
echo '<pre>';
print_r( $meta );
echo '</pre>';
Or for a single value:
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );
See below for more information in the WordPress Codex:
https://codex.wordpress.org/Custom_Fields
and
https://developer.wordpress.org/reference/functions/get_post_meta/

Related

Custom category template WP

I have created a custom post type (activity) with category taxonomy , In the menu the user can select which category he want to display , the problem is that I created category.php but nothing appears on my site here is the code :
<h1><?php single_cat_title(); ?></h1>
<?php if (is_category('edition')) :
$args = array('category_name' => 'edition');
else :
$args = array('category_name' => 'diffusion');
endif; ?>
<?php $query1 = new WP_Query( $args );
if ( $query1->have_posts() ) {
while ( $query1->have_posts() ) {
$query1->the_post();
echo '<p>' . get_the_title() . '</p>';
}
}
?>
I think you can call your file archive-activity.php to make it work .
For your custom post type it's archive-{post-type}.php, and keep the same code you've posted.
The archive-{post-type}.php template provides the most general form of control, providing a layout for custom post type archives, a page that displays a list of posts.
To use different template for your special taxonomy create a file call taxonomy-{taxonomy}-{term}.php or tag-{slug}.php to target specific term.
You can read more about the template hierarchy here : custom-post-type-template-files

Wordpress Posts Id from query_posts

I'm using WordPress with a template that generates a pretty nice thumbnail for each post depending on Ids, and type of post. (ref:https://blinkdemo.wordpress.com/)
Since I've been asked to create a custom page that could show certain post from a category, I decided to create a query for a template page that checks the page slug and then list the posts containing a certain category + a tag ('comparativas').
The problem that I'm facing is that the list of post presented on the page doesn't show up the corresponding thumbnail on each post.
Thumbnails are generated dynamically basically with these lines:
$post_id = $post->ID;
$thumbnail_id = get_post_thumbnail_id( $post_id );
$thumbnail_image = wp_get_attachment_image_src( $thumbnail_id,$thumbnail_size );
The problem is that I couldn't find the way to send to the specific post ids to the function above, since the main $wp_query->posts; retrieves the page id instead of the post requested by the query_posts method.
The loops present the correct posts but when I echo the post->ID it shows the page id.
My query is:
global $wp_query;
// concatenate the query
$args = 'cat='.$idCategory.'&tag=comparativas';
query_posts( $args );
$posts = $wp_query->posts;
$current_id = get_the_ID(); //-> this returns the page id
If you could please tell me how to overwrite the global $wp_query; so the template can handle the corresponding ids for the list of post It would be great.
Any clue?
Best,
Juan
You could use, setup_postdata($post)
Then get_the_ID() works again :)
It doesn't work just because you aren't looping through them. You can do it many ways.
The 2 more common are the following:
overwrite the query with query_posts
$args = array('cat' => $idCategory,'tag' => 'comparativas');
query_posts($args);
if(have_posts()){
while(have_posts()){
the_post();
$current_id = get_the_ID(); // this return what you want now
the_title(); // this works as expected
}
}
wp_reset_query(); // get previous query back
get an array of WP_Post objects and setup_postdata or simply loop through them
$args = array('cat' => $idCategory,'tag' => 'comparativas');
$posts_i_want = get_posts($args);
foreach( $posts_i_want as $post ){
setup_postdata($post);
$current_id = get_the_ID(); // this return what you want now
the_title(); // this works as expected
}
wp_reset_postdata(); // get previous postdata back
I personally prefer the first in most cases
Cheers

Getting Only Custom Post Types Posts with WP_Query

I am trying to get custom posts with WP_Query but it's not returning only custom post type posts but also default posts too.
I am using
$args = array (
'post_type' => array ('survey')
);
$sPosts = new WP_Query($args);
as a result I am getting 'survey' posts as well as default posts, I only need it to return 'survey' posts.
P.S. when I use query_posts() it returns the required result, but just not getting it done with WP_Query and I prefer to use WP_Query not the query_posts()
Please try it like that....
<?php
$new = new WP_Query('post_type=discography');
while ($new->have_posts()) : $new->the_post();
the_content();
endwhile;
?>
I believe this custom query should actually be your main query, in which case you should not use a custom query
The problem you are facing is either due to
Wrong use somewhere of pre_get_posts. Remember, pre_get_posts alters all instances of WP_Query, backend and front end. Lack of correct use will break all queries
You have not changed the loop to be objects of your new query
To come back to the point, as said, this is suppose to be the main query, so lets target that problem.
The first thing to do would be to delete the custom query. Once you have done this, you would only see normal post.
We are now going to use pre_get_posts to alter the main query to only show custom posts. Paste the following inside your functions.php
add_action( 'pre_get_posts', function ( $q ) {
if( !is_admin() && $q->is_main_query() && $q->is_home() ) {
$q->set( 'post_type', 'YOUR CPT' );
}
});
You should now just see post from your cpt on the homepage
EDIT
Your index.php should look something like this
if( have_posts() ) {
while( have_posts() ) {
the_post();
// HTML mark up and template tags like the_title()
}
}else{
echo 'No posts found';
}
This code can get all the posts in a custom post type..,
wp_query is the function can get all the posts in the custom post type. wp_query requires array, so you can give the custom post type name in post id to array
$args = array(
'post_type' => 'address', //custom post type
'posts_per_page' => -1 // get all posts
);
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
echo( get_the_title() );
}
}

Advanced custom field (wordpress plugin of same name) not showing

I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta

Wordpress loop - Display posts by tag from differing post types

I'm trying to display posts within the WP loop and am able to successfully do so with <?php query_posts('tag_id=10'); ?>Here the loop will display all posts with the tag ID of 10, but I'd also like the loop to display posts from within a Custom Post type by the same tag.
I'm able to successfully display posts with tag_id=10 that originate from a custom post type using <?php query_posts('tag_id=10&post_type=videos'); ?>
But how can I merge the two?
I gave this a shot: <?php query_posts('tag_id=10, tag_id=10&post_type=videos'); ?>
but that had no effect.
Any ideas on this one?
You can use this
query_posts(
array(
'post_type' => array('post', 'videos'),
'tag_id' => 10
));
while (have_posts()) : the_post();
// loop code
endwhile;
wp_reset_query();
This runs an action before the posts are actually queried, thus altering the original output to your specific needs.
function tag_archive_mod( $query ) {
if ( is_tag() && $query->is_main_query() ){
$query->set('post_type',array('post','video'));
}
}
add_action('pre_get_posts', 'tag_archive_mod');
Very, very useful.
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Resources