WordPress: Editing custom post type - php

I've to do some edits to wordPress theme home page . This is the scenario.
There are 4 posts being showed in home page under news tab now when i check this is using front-page template and in front-page template I've something like this
<?php
wp_reset_query();
$loop = new WP_Query( array( 'post_type' => 'news', 'posts_per_page' => 4 , 'orderby ' => 'date' , 'order' => 'DESC' ) );
// $loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
Now when i try to find out post type for news i dont find it .so if there is no custom post type for this what could be other option for this? please someone help me with it how can i figure this out .

Related

Using a Custom Post Type in your Website Manually

Using the code below added to the functions.php file, I have manually created the custom post type, 'Products'.
How can I write a custom function to show the created products on all pages of my website? Which file would I add this to?
(I know there are plugins available to do this, I'm trying to learn the old-fashioned way first)
Thanks!
<?php
add_action('init', 'prowp_register_my_post_types');
function prowp_register_my_post_types()
{
register_post_type(
'products',
array(
'labels' => array('name' => 'Products'),
'public' => true,
)
);
}
One way of doing it is to make a custom query on the template file.
Let us assume your template file is running on index.php or single.php to show the content then querying like this should do the work after registering your custom post type
$args = array( 'post_type' => 'Custom-Post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="Custom-content">';
the_content();
echo '</div>';
endwhile;

Show custom post type category posts on the footer

Can you please guide me how to show certain category posts on the footer. I am using custom post type news-site, taxonomy is news-category.
The link structure is abc.com/news-category/politics-news. The Politics news is a category page and all politics related news showing on the category page.
I don't know how to show 5 recent posts with same category on the footer.
I have tried with tag_id but nothing is showing.
Also i have tried this related post Related post but didn't work
Can you please guide me
Thanks
You can get the 5 posts of custom taxonomy by using the following logic.
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
$term_id = esc_html( $categories[0]->term_id );
}
$args = array(
'post_type' => 'news-site',
'post_status' => 'publish',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'news-category',
'field' => 'id',
'terms' => $term_id
)
)
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_title();
endwhile;
?>
Don't forget to pass taxonomy_name in the query

Ads between posts in Wordpress with Genesis Framework

I am trying to set up a blog that inserts an ad between each post. Here's a wire frame to help explain what I am trying to do. (wireframe) Ads are outlined in red. I am using Genesis framework.
I tried using custom post types to create an "Ad" post type but the problem is I can't prevent the ads from duplicating on the same page. I use the
Here's my loop that I am using. I run the genesis_after_entry hook to add the loop.
<?php $args = array( 'post_type' => 'ads', 'posts_per_page' => 1, 'orderby' => 'rand' );
$do_not_duplicate = array();
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$do_not_duplicate[] = $post->ID;
the_content();
endwhile; ?>
Something's obviously wrong because it's still duplicating. Any help would be greatly appreciated!

WP Query - Unable to query by category slug

I have four categories in my Custom Post Type of "projects" that are 'live', 'heal', 'work', and 'play'. I have a few 'projects' posts with no category that I would like to omit from the archive page. In my archive, my query is as follows:
<?php $args = array( 'post_type' => 'projects','post_status' => 'publish', 'posts_per_page' => $projects_number, 'paged'=> $paged, 'category_name' => 'work,play,live,heal'); ?>
<?php $wp_query = null; ?>
<?php $wp_query = new WP_Query( $args ); ?>
<?php if ( $wp_query->have_posts() ) : ?>
But my archive is not returning any projects. I also tried the 'cat' parameter with id's but no avail there either. What am I missing?
For anyone who comes across this...the 'category' argument needed to be in its own array

Custom post types displaying all category posts instead of showing posts only from specific category

I am having issues with custom post type categories display.
I have created custom post type for review site
I want to show different categories in different tabs
but when I put any category of review in menu it shows all reviews
instead of showing reviews from specific category
For example:
I have created 2 categories in reviews
a) Games
b) software
Whenever I choose Games category it shows posts from software category too.
I had same issue with blog posts categories but I resolved that issue using code
in my category.php file
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat_id = get_cat_ID( single_cat_title(null, false) );
query_posts(array(
'post_type' => 'post',
'paged' => $paged,
'cat'=>$cat_id,
));
I have created taxonomy.php file for custom post type
<?php $mypost = array( 'post_type' => 'cpreviews','paged' => $paged);
$loop = new WP_Query( $mypost ); ?>
Can anyone please help us to understand what we need to do to display posts
according to categories for custom post types?
UPDATED CODE IN TAXONOMY.PHP but still have some issue:
I have changed above code under taxonomy.php to
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//$currentTerm = $_GET[ 'term' ];
$cat_id = get_cat_ID( single_cat_title(null, false) );
$mypost = array('post_type' => 'cptreviews',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_reviews_product_category',
'terms' => (''),
'field' => 'slug',
'name' =>'Product Category',
)
)
);
$loop = new WP_Query( $mypost ); ?>
Now whenever I put category in 'terms' => ('kids') like this it shows all posts under that category only. but I want to take that 'terms value' dynamically.
Try this one:
<?php
$type = 'cpreviews';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Supposing you have Custom Post Type: cpReviews --- Custom Taxonomy: RevCategories --- Create new review post and chose category from RevCategories. Query cpReviews will definitely show all the posts, you need to do some thing like this -----
query_posts(array(
'post_type' =>'cpreviews', //Custom_Post_TYpe
'showposts' => $limit,
'RevCategories' => 'Games',)); //Custom Post Type TAxonomy (can use page name here get_query_var('pagename'); for dynamic content
while (have_posts()): the_post(); global $post; echo the_title(); endwhile;
I have resolved this issue by creating taxonomy-{taxonomy}.php file & removed tax query code..it automatically takes given category..thanks all for your help
This will resolve this issue.
$args = array(
'post_type'=> 'post',
'cat' => 'Games'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

Categories