i'm developing a web portal using wordpress.
I need to access draft posts from frontend.
If i login with administrator account i can view draft posts(admins posts) in single pages. But other users can't access their own drafts from frontend and gets 404 error.
The links on frontpage like http://website.com/?p=486
I'm not sure whats the problem about. Maybe access restrictions or permalink settings or 404 settings or something else. Any ideas?
I found the solution. The problem is single page only shows published posts.
With using wordpress pre_get_posts hook i changed the query and add also draft posts.
function ceo_single_page_published_and_draft_posts( $query ) {
if( is_single() ) {
$query->set('post_status', 'publish,draft');
}
}
add_action('pre_get_posts', 'ceo_single_page_published_and_draft_posts');
The solution that works for me is to query for published and draft:
query_posts(array(
'post_status' => array( 'publish', 'draft')
));
Related
I'm trying to accomplish something that shouldn't be too difficult. However, I cannot get it right. The situation is as followed: I have a multisite with 2 subsites. All the 3 sites have their own custom post types.
I am trying to achieve the following: when a custom post type post is create on site ID = 3 using an ACF Frontend Form, this automatically creates another custom post type post on site with ID = 2.
Using the code below I am able to cross-post the posts from site A to site B. However now I only need to send the title through the Front-end form. The updated code can be found below:
add_action('publish_holiday', 'add_surf_holiday', 10, 2);
function add_surf_holiday($post) {
switch_to_blog(2);
/* the rest of the function code */
$labels = array(
'post_title' => $_POST['acf']['name_surf_accommodation'],
'post_status' => 'publish',
'post_type' => 'surf-holidays'
);
wp_insert_post( $labels );
restore_current_blog();
}
Does any of you have an idea how to solve this issue?
If anything is unclear, please let me know.
Hope to hear from you soon!
Cheers,
Hans
The issue maybe with where you have this code, and I'm making the assumption that you have placed it in functions.php and if so then you should remove it from there and then create a plugin with your code and Network Activate it. Have you tried this?
EDIT: Found my old code, that worked, and got it fixed that way. Also gonna use a plugin to add custom functions, to avoid it dissaperaing again with next theme update (Thanks Heba). And the code:
function foo_modify_query_order( $query ) {
if (get_the_ID()==80) { $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' ); } } add_action( 'pre_get_posts', 'foo_modify_query_order' );
So, it should be a simple task, but I have no idea why it's not working by now. I have tried everything, every Google link is purple, so now I hope someone can tell me what it is, I'm doing wrong.
I should start by mentioning that it's a Divi theme, but I did get it to work a few months ago, but forgot how, and I can't recreate it now.
I'm just trying to sort the main query alphabetically (A-Z), but only on a page called recipes. The site have all the recipes as posts, together with their blog posts, but the blog posts are sorted by date and showed on a separate page, which works fine.
But weird stuff are happening, when I try and alter it. If I use this code, it actually works, but the blog page also get sorted alphabetically, which it shouldn't.
function foo_modify_query_order( $query ) {
if (is_archive()) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'foo_modify_query_order' );
Which doesn't make sense to me, since it's posts? Out of all the ways I've tried, it only sorts if I use is_archive().
If I try to make it check for a specific page first, like && is_page('recipes') I get an error.
If I just add if (is_page('recipes')){ echo 'Recipes'; } it works, but if I try to add the sorting inside, I get nothing.
So partially working, but I can't combine the two. Is it a Divi issue or am I doing it wrong? Hope anyone can help, I've tried so many different things now. Cheers :)
I would recommend creating a child theme where you will create a new page template to be used in the 'recipes' page.
First, use Child Theme Configurator plugin to set a new child theme, don't forget to select Copy Menus, Widgets and other Customizer Settings from the Parent Theme to the Child Theme option to keep your settings:
After that, from files tab in the child theme configurator, copy the page template used for blog (or the page template used in 'recipes' page).
After that, change the file name, and the name written in the top of the page template file after Template Name, to recipes ordered alphabetically or any name you would prefer like next:
/*
* Template Name: recipes ordered alphabetically
*/
You can add these args to the query:
$args['orderby'] = 'title';
$args['order'] = 'ASC';
And change 'recipes' page template to the new created one.
For more detailed answer, Please insert the code used in the page template.
I'm having an irritating problem with my Wordpress site I was hoping you might be able to help me with.
Basically, I have a page - Case Studies, which was called 'case-studies' in the nav structure. It was later removed and re-added, forcing it to 'case-studies-2'. Is there any way of editing this? I tried changing the permalink within the page itself but it reverts to 'case-studies-2' after being changed.
My second issue is with the custom post type 'Case Study' which ties into this page. I can get the page to pull the custom posts in a list, but their permalink goes to a 404 and resetting the permalink structure didn't fix it!
Lastly, my attempts to resolve this problem have led to my Case Studies page bouncing back to the homepage no matter what I do with the template file.
Thanks in advance folks,
Graham
The first page of Case Study with nav-structure case-studies is still available in trash of post so firstly delete that page from trash & then edit your new case study page and in that edit permalink and remove 2 just like case-studies-2 to case-studies
For second issue you have to register your custom post type to theme's function.php file
For this add this code to function.php file
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'album', 'movies','music_review') );
return $query;
}
after this reset your permalink
Issue resolved - the old file was in the bin, not properly removed. Deleting it from there allowed me to update the permalink directly on the page :)
Thanks for your help, peeps!
I have created a custom post type using PODS
The URL ends up being:
http://example.com/dj/dj-name
The dj has an author field that I am using to query the DJ's posts.
The url for next_posts_link(); ends up becoming /dj/dj-name/page/2 as one would expect.
After clicking the link wordpress seems to be ignoring the /page/2 and just redirecting back to /dj/dj-name
My permalink structure is: http://example.com/sample-post/
When I use default permalink structure /?dj=dj-name&paged=2 it works correctly.
Any ideas?
In your PHP file, do you assign $paged to anything? View Pagination Codex
Above your query, probably right below your get_header() you need to add something like this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
If that doesn't work you can try saving your Permalinks again (Settings -> Permalinks -> Save Permalinks). This will refresh your current permalink structure.
Finally, if you have a page called "dj-name" already created, you can run into problems because WordPress can't differentiate between a subpage called page/2/ and a pagination link page/2/
Pagination only works on archive pages such as index.php, category.php, archive-posttype.php etc.
You can make it work on single.php or similar pages but you need to hijack the built in redirect function, see below...
function dj_redirect_hack($url) {
if ( is_single() and get_post_type() == 'dj' ) $url = false;
return $url;
}
add_filter('redirect_canonical','dj_redirect_hack');
I have a plugin that automatically expires posts and changes it's post status to "archive" on a certain date.
We then have an archive section that houses all these posts. I still want to display the data for the post but it gives a page not found.
Can I alter the query to display this post on the single template?
I've tried the following but it pulls in all archive posts rather than just the page you're on.
<?php
$my_query = new WP_Query('post_status=archive');
?>
<div>
<?php
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
?>
<ul>
<li>
<?php the_title(); ?>
</li>
</ul>
<?php endwhile; else: ?>
<div>
<ul>
<li><?php _e('No upcoming Posts'); ?></li>
</ul>
</div>
<?php endif; ?>
</div>
Post status allows users to set a workflow status for a post in WordPress.
There are 8 default statuses that WordPress uses. They are published, future, draft, pending, trash, auto-draft, and inherit.
A post may also have a new status if it was just created and hasn't had any previous status.
WordPress themes and plugins can also define custom post statuses for more complex websites. These statuses allows users to organize their posts in the admin panel. They are especially useful for sites with multiple authors or a complicated editorial process.
Some things that post status allows is for users to work on an article without publishing it and saving it as a draft. This way they can go back later and finish it. It also allows users to schedule posts which gives the post a status of future, or make a post private. Attachments have a post status of inherit. For multi author blogs the pending status can be useful as contributors can submit posts for review prior to publishing.
In your case, you have to be 100% sure that your archived posts get status archive, otherwise your WP query will not work, so check if this plugin you use set this post type (the easy way is to look directly in MySQL database, table wp_posts or, if you do not have access to the MySQL server via PHPMyAdmin - then look at the source code of the plugin itself.).
EDIT: Can you try, just for testing, to change your WP query like this:
$my_query = new WP_Query( array( 'post_status' => array( 'pending', 'archive', 'publish' ) ) ); and see the result?
The reason why you get all archived posts is because you're querying an entirely new query with the post_status=archive.
To solve this, I've been trying with some code to utilize WordPress Action API like pre_get_posts but it doesn't work properly for me mainly because WordPress has already thrown a 404 error.
So it might be a better idea to override this in the template like in single.php.
<?php
$post_id = get_query_var('p');
$args = array(
'p' => $post_id,
'post_status' => array( 'publish', 'archive' )
);
$my_query = new WP_Query($args);
?>
I might suggest to try this out first. As I mentioned, you were getting all the posts in archive. In this case, you are getting the post ID and adding that into the query. Therefore, you'll be getting on the current post and with publish and archive post_status so that all posts with both post_status will be queried.
Unfortunately, in my testing, it just doesn't work on visitors' who aren't logged in. It'll work for you who's logged in.
Let me hear about the result in the comment.
Display post by ID:
$query = new WP_Query( 'post_status=archive&p=7' );
Display page by ID:
$query = new WP_Query( 'post_status=archive&page_id=7' );
Thanks to #josephting & #bodi0 for getting me thinking in a different way, actually solved it without adjusting the single template from the original loop by using this (I'd used this on another site to get future posts to show):
add_filter('the_posts', 'show_future_posts');
function show_future_posts($posts)
{
global $wp_query, $wpdb;
if(is_single() && $wp_query->post_count == 0)
{
$posts = $wpdb->get_results($wp_query->request);
}
return $posts;
}
With all other solutions either it was still saying page not found or wasn't displaying the data fully.