I've got this script to list all the post titles with permalinks including posts that are in the trash, on the front-end:
<ul>
<?php
$myposts = get_posts(array(
'numberposts' => -1,
'offset' => 0,
'category' => $cat_id,
'post_status' => array('publish','trash')
)
);
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</ul>
This works fine. But the problem is if I click on any of the post titles that are in the 'trash', I get the 404 page.
How can I access trashed posts on the front-end? I understand this the default Wordpress behaviour, but is there perhaps a function that allows trash posts to be viewed?
Thanks in advance.
By default, only published posts are displayed by the main query for all users and additional private posts for logged in users. So we can use the pre_get_posts hook to add an additional post status to the main query
This is totally untested, not sure it will work, but you can try the following
add_action( 'pre_get_posts', function ( $q )
{
if ( $q->is_main_query()
&& $q->is_single() // can replace with $q->is_singular()
) {
$q->set( 'post_status', array('publish','trash') );
}
});
Related
I'm looking to modify WordPress search where based on the post type in the search results, either the post title will be linked to its detailed page or to a 3rd party website.
For instance, post type - 'publication' has a detail page where a user can read the publication in detail when they click on the post title. However, the post type - 'media', doesn't have a detail page but is linked to a 3rd party website and the 'website url' is a custom field.
Post type- publication -> detail page
Post type- media ->3rd party website (website url)
Both the publication & media are custom plugins that we have created.
Here is the code I tried to write to get the custom field 'website_url' and link it to the the_title() but it doesn't work. It doesn't go into the while ( $the_query->have_posts()) loop.
If (post_type=="media")
{
$args = array(
'numberposts' => -1,
'post_type' => 'media',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'title', //post link
'compare' => '=',
'value' => $posttitle
)
)
);
// query
$the_query = new WP_Query( $args ); ?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
<?php
$link = get_field('website_url'); ?>
<div><h3><?php the_title(); ?></h3></div>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php
} //for each loop
} //if closes here
}
}
else
{
<?php the_title(); ?>
}
The result should be where when it is a media post, clicking on the post title will take the user to "website url" else to the detailed page.
well you can just hook in "pre_get_post" and check for the post type there. Something in this fashion:
add_action( 'pre_get_posts','search_filter' );
function search_filter( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
if ( $query->get( 'post_type' ) == 'media' ){
//change the post link here
}
}
}
}
I hope this helps for more information please see this - https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
Good afternoon all,
I have a custom taxonomy called 'Gallery' and a have created a new page template to pull in and paginate all posts in the 'gallery' taxonomy.
This works fine (as a page), however I would like to set this page as the WordPress static 'Front page'.
When I set this page template as the 'Front page' the pagination no longer works. I have tried many solutions today and would really appreciate some help on this one!
Any help/tips massively appreciated!
Thanks.
My Code:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => 'gallery',
'paged' => $paged,
'orderby' => 'menu_order',
'order' => 'DESC'
);
query_posts( $args );
if ( have_posts()) : while( have_posts() ) : the_post(); ?>
<!-- List Posts -->
<?php endwhile; ?>
<nav>
<?php previous_posts_link( __( 'Previous', 'framework' ) ); ?>
<?php next_posts_link( __( 'Next', 'framework' ) ); ?>
</nav>
<?php else : ?>
<!-- No Posts -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
EDIT:
If I add global $paged; before the if statements it does works perfectly. Could anyone educate me on what I was missing?
Also what are the performance implications (if any) of calling global $paged; on the homepage?
Add global $paged; before any code runs.
global $paged works throughout the WordPress, to access this we need to put it before usage like your case is. Yes, there is no issue at all to use this variable and you are in totally safe hands. Actually, $paged variable is getting posts for each page and once you are using this variable its showing up data appropriately.
There are other global variables as well to be used in WordPress, for whole list you can visit this official codex page https://codex.wordpress.org/Global_Variables
I hope after reading this comment and going through this URL your all points will be solved and you can utilize all this info in future as well with best approach ...
How do I get a random post in Wordpress?
I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don't want a random post to be displayed on the page, I just want a link that leads to that post.
I tried searching for a code on Google and here at stackoverflow but no success.
Thanks...
UPDATE:
Here is my template code:
<?php /*Template Name: Random*/ ?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-archive">
<div class="grey-text">Random post</div>
<?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>
<?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
?>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
create a page template, and use the following code to get a random post:
//Create WordPress Query with 'orderby' set to 'rand' (Random)
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
// output the random post
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
then in a page, just use:
see a random post
I found this post which gave me desired results...
Here's a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.
Just add the following code to the functions.php file:
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
Use mydomain.com/random/ as your href for your button that leads to the random post.
Thanks everyone who contributed for your help...
Cheers!
I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it's really easy, for a blog at
http://mygroovywpsite.me/
All you need to do is append it with ?random
http://mygroovywpsite.me/?random
I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.
First make a Page in your site called "Random" / with the slug "random" -- it does not need any content in it
Then create a page-random.php template
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
Then you get the re-direct for any link on your blog ...../random w/o any wrestling with .htaccess
I've done it this way because I've had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.
I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()
Another Simple solution to display Random Post
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
Check This
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
I have this code below that querys posts and a post type:
<?php
$args = array('post_type' => 'apartmentlisting', 'parent' => 0, 'showposts'=>'-1');
query_posts($args);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
I am trying to query and return only the parent pages I have 10 parent pages and each have about 4-5 child pages. Any ways to return just the parents?
I have been digging on the codex on WP and on google and nothing. I only found articles on returning posts that have a parent of page ID XX.
Any ideas?
If there is no parent, the parent is zero. So your query should work. But the parameter is 'post_parent' not 'parent'. And 'showposts' is deprecated, use 'posts_per_page' instead. So try this:
$args = array('post_type' => 'apartmentlisting', 'post_parent' => 0, 'posts_per_page'=>'-1');
Have you looked into using the following:
<?php get_post_ancestors( $post ) ?>
where $post is the post id? You can get the post's ID with the following if you are within an action hook for the_content:
global $wp_query;
$page = $wp_query->page->ID;
I'm trying to create a navigation based around post titles. The main navigation at the top is the Categories, in a sidebar will be the Post Titles from that relevant Category.
The code I'm using works up until a point (i got it from List wordpress posts by category which match page title).
But I have 2 problems.
The main issue is when you get to the single post it just lists that post in the sidebar and no others from the same category.
The second minor issue, is it is listing pages when I don't need it to.
<p>below lists the right posts for the right
cats but fails at the single post stage</p>
<?php
$test = get_the_title();
$args = array( 'cat_name' => $test );
$args = array_merge( $args , $wp_query->query );
get_posts( $args ); while (have_posts()) { the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<ul>
<li><?php the_title(); ?></li>
</ul>
<?php } ?>
Any help would be appreciated. Thanks
for you second issue you can add one argument 'post_type' => 'post'
it will only display posts.
$test = get_the_title();
$args = array( 'cat_name' => $test,'post_type' => 'post' );
$args = array_merge( $args , $wp_query->query );