Show tags that are the same as the title of the custom post wordpress - wordpress

I'm at a loss. On my single{custom-post-type}.php I want to create a loop that first shows the title of the page and the content. But then it also generates a list with all the content from a tag that had the same name as the title of the page.
Can somebody help me on the way?

You need an instance of WP_Query to query the posts inside the taxonomy term with the same name as the title. This is done by adding the tax_query field to the arguements.
<?php
// WordPress header
get_header();
the_post();
$args = array(
'post_type' => 'custom-post-type',
'posts_per_page' => -1, // -1 retrieves all the posts the query finds.
'tax_query' => array(
array(
'taxonomy' => 'category', // This is the default 'post' taxonomy, if you are using a custom taxonomy then you need to change this.
'field' => 'name', // Use name as you want to match the title
'terms' => get_the_title(), // Get the title of the current post as a string.
)
),
);
$query = new WP_Query($args);
if($query->have_posts()):
while($query->have_posts()): $query->the_post(); // Loop through the posts from the term with same name as current post title. ?>
<article class="post">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile;
wp_reset_postdata(); // Reset usage of 'the_post()'
endif;
<?php get_footer();

Related

user comments display on wordpress page

looking to achieve something similar to this page where user can enter a thought into a form field and on submission this then posts straight onto the page.
http://hcma.ca/
Would using wordpress comments be best for this? Or some way of sending the form submission to populate a repeater advanced custom field on the page. Can anyone advise how best to achieve this?
Also wondering about spam. The site above has no captcha or similar (as far as I can tell). What's the deal with this?
Thanks!
If you don't want to use a paid plugin, you can create a new Custom Post Type and then display the results on the page you want. You should follow this tutorial here
In short:
Create a new post type:
// Our custom post type function
function create_posttype() {
register_post_type( 'movies',
// CPT Options
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'movies'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
Display the results in a page:
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
You can use then this free plugin to add Google reCaptcha to avoid spam

Pagination shows empty pages for date based archive.php loop

I'm using wordpress and have a custom archive page for a custom post type.
The custom loop gets the logged in users registration date and only shows posts that were published on or after they registered. Works great.
However, the pagination is still using the main query so if there are 4 posts in total set to 2 posts per page the pagination always shows there are two pages even if only one post is displayed due to the logged in users registered date.
Can anyone help me modify what I have so the pagination only shows for results in more than 2 posts for that users query? I've been trying for hours now using various changes I've found on the web...
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
Working with Custom Archive and Pagination
#Scott Eldo Your approach with create custom query will not change main query on your custom archive. FYI and you are correct, pagination only work for main query.
In your case, the recommended approach, I will use filter pre_get_posts to work with custom archive and pagination. Please take a look my answer here how to modify main query on post type archive page, you can figure it out with your query parameters.
BUT if you intent to create query direct into your template ( even it is not change main query ), you need to match your custom query with $GLOBALS['wp_query'] that use in pagination and don't forget to use wp_reset_query() ( MUST ). Take a look my approach here related with your code:
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<?php
/**
* Fix Pagination in custom page/archive
* Set global wp_query the same as our custom query
*
* Use function the_posts_pagination( $args ); for pagination will work too
* see https://developer.wordpress.org/reference/functions/get_the_posts_pagination/#source-code
*/
$GLOBALS['wp_query'] = $my_query;
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
<?php wp_reset_query(); ?> // MUST use to reset global query and post data
Another approach and still NOT recommended is use query_post for your custom query. More reading about it you can learn more in here.

Show post type where category equals page title

I'm trying to list posts from a custom post type 'article' on several pages where the post category matches the page title. So on the products page there would be list all the articles with the category products, reviews page there would be a list of all articles with the category reviews, etc.
No posts are returned when I use category_name, and all posts are returned without it.
<?php
// Get articles that have a category that matches the page title (ie: On pagination page get articles with pagination category)
$pageTitle = get_the_title();
$postType = 'article';
$args= array(
'post_type' => $postType,
'post_status' => 'publish',
'category_name' => $pageTitle
);
$articles = new WP_Query($args);
if( $articles->have_posts() ) {
while ($articles->have_posts()) : $articles->the_post(); ?>
<p><?php the_title(); ?></p>
<p class="tags"><?php the_category()?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
The 'category_name' parameter requires you use the category slug, not the name. You could get the category ID instead from the name...
$catID = get_cat_ID( $pageTitle );
... then use the 'cat' parameter in your query.
'cat' => $catID,

List custom post type with certain tag (wordpress)

So this has been driving me nuts.
I can easily list all post titles that have a certain tag, like this:
<?php
query_posts( 'tag=products' );
if ( have_posts() ) while ( have_posts() ) : the_post();
echo "<h1>" . the_title() . "</h1>";
endwhile;
wp_reset_query(); ?>
But this is just for all standard posts. What I need to do instead if make this work for a certain custom post type only (my custom post type is called 'products'). In other words, it should display the titles of all 'products' custom post types that also have the tag 'webonly'.
Any suggestions will be helpful.
display all list of post which in add webonly tag
notice-tag = your tag taxonomy name
terms in define your tag slug name
'post_type'=>'notice', in define your post type
<?php
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'notice-tag',
'field' => 'slug',
'terms' => array( 'webonly' )
)
),
'post_type'=>'notice',
'order'=>'ASC',
'posts_per_page'=>-1
);
query_posts($args); ?>
<?php while (have_posts() ) : the_post(); ?>
<?php echo the_title(); ?>
<?php endwhile; ?>

Wordpress - Sorting post loop by meta data date

I have two CPTs, one called 'artist' and the other called 'release.' I've created a single-artist.php page that displays an artist and its' custom meta data. On that same page I am displaying all releases by that artist with the following code:
<!-- GET RELEASES -->
<?php
$category = get_the_category();
$artist_name_slug = $category[0]->slug;
$args = array ('post_type' => 'release', 'posts_per_page' => 20, 'category_name' => $artist_name_slug);
query_posts ($args);
?>
<?php if (have_posts()) : ?>
<h3 class="artist-col2-title">Releases</h3>
<?php while (have_posts()) : the_post(); ?>
<div class="artist-release"><?php echo the_post_thumbnail('small'); ?></div>
<?php endwhile; ?>
<?php endif; ?>
<div style="clear:both;"></div>
Within the release CPT I have a release date in the meta data.
I would like to sort the releases based on that date but I cannot figure out how to add that to my arguments. Any help would be greatly appreciated!
To sort by meta data you can use
$args = array (
'post_type' => 'release',
'posts_per_page' => 20,
'category_name' => $artist_name_slug,
'meta_key' => 'your_meta_key' // i.e. release_date
'orderby'='meta_value' // for numeric value use 'meta_value_num' instead
);
query_posts ($args);
But notice the meta_key should be present in the query that you want to use for sorting, see for more.

Resources