Show only authors post except a single post ID - custom-post-type

I am working with Wordpress, and I have a post type using ACF fields.
The fact is that I used a function in function.php to show only author's posts but I completely forgot that It wont show the ACFgroupfields
Here is my function
function posts_for_current_author($query) {
global $user_level;
if($query->is_admin && $user_level < 5) {
global $user_ID;
$query->set('author', $user_ID);
unset($user_ID);
}
unset($user_level);
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
How to make an exception for an ID for a post ?
Thank you very much !

Might be a bit late to respond to this. But what you could try is something like this.
add_action( 'pre_get_posts', 'pre_check', 10, 2);
function pre_check($query) {
global $user_ID;
if ( !current_user_can( 'edit_others_posts' ) && $query->query['post_type'] != 'acf-field-group' && $query->query['post_type'] != 'acf-field') {
$query->set('author', $user_ID);
}
return $query;
}
This way it will not list posts/pages other than what belongs to the author. And still display the ACF fields on the posts/pages itself.

Related

How do I make puppeteer wait for element to load (in a loop)?

I am trying to use puppeteer in order to fill in a form and get the results.
when the form is submitted correctly, a table containing the results appear which has #some_id. Now I'm looking for a good way to wait until the table is loaded and if the code fails, redo the process of filling in the form until it works correctly. I want it to do something like this (pseudo code):
while(table_is_not_loaded){
get_information;
fill_in_the_form;
submit_the_form;
}
I think that it's maybe achievable using page.waitForSelector() function, but I can't use it the way I want and I also don't know how it handle errors if the selector is not ready or visible.
You could do something like this:
let table_is_loaded = true;
await page.waitForSelector('someID').catch(e => table_is_loaded = false);
while(!table_is_loaded){
get_information;
fill_in_the_form;
submit_the_form;
table_is_loaded = true;
await page.waitForSelector('someID').catch(e => table_is_loaded = false);
}
Maybe something like:
while(true){
try {
await page.waitForSelector('#foo')
get_information;
fill_in_the_form;
submit_the_form;
break
} catch(e) {
}
}
after some searching and trying different pieces of code , i came to a solution and it was using the.$eval() method to access html attributes and checking the height of the element( you could also check other things as well like display or visibility )
const isNotHidden = await mainPage.$eval(
"#some_id",
elem => {
return (
window.getComputedStyle(elem).getPropertyValue("height") >= "5px"
);
}
);
link to main answer that helped me achieve this : https://stackoverflow.com/a/47713155/11968594

Better URL formatting with Custom Post Types and Taxonomies

I'm using Toolset Types and wondering how easy it is to set up the URL's how I want.
I have a custom post type of venues and I have a custom category taxonomy of location.
Currently the urls are coming out like
http://domain.com/venue/location/manchester/
http://domain.com/venue/manchester/the-venue-name/
But I want the URL's to be structured like
http://domain.com/manchester/
http://domain.com/manchester/the-venue-name/
Where do I need to look to make these changes?
Is this all .htaccess work or can something be done within the permalinks section?
Thanks in advance
If i understand right, this hack must work in your template.
First of all we have to remove the Post type name from url Slug.
function ft_remove_postType_slug_fromUrl( $post_link, $post, $leavename ) {
if ( 'venue' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'ft_remove_postType_slug_fromUrl', 10, 3 );
But this is not gonna work by itself. If u pate this code in your functions.php u should get 404 Error.Bbecause WordPress only expects posts and pages to behave this way.
So, you have to add this action also.
function ft_change_parsingRequest( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'venue', 'page' ) );
}
}
add_action( 'pre_get_posts', 'ft_change_parsingRequest' );
After this, you renew / refresh yours post type / permalink tree (It calls flush_rewrites i guess.), It means, re-update your permalink settings on your admin panel area.
Or if you want to see or do some magic, you can check it out from source url.
https://core.trac.wordpress.org/browser/tags/4.3/src/wp-includes/post.php#L1454
This Line says;
add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
Happy Coding.

How to display flash message in Kohana 3

I have to show message after insert some data in database. I'm using Kohana. Is there a way to do that with flash messages? It's better than header refresh.
Well sort of. You could use the Session::get_once() function. But this only let you retrieve a variable once, and you cannot use it again in the same request. While you want a flash message to persist a full request cycle. To manage that you'll need a wrapper class, something like this.
class Flash {
private $session;
private $messages = array();
private static $_instance; // Singleton object
public static function instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new Flash();
}
return self::$_instance;
}
private function __construct() {
$this->session = Session::instance();
$this->messages['current'] = $this->session->get_once('flash');
if( ! is_array($this->messages['current'] ) ) {
$this->messages['current'] = array();
}
}
public function add( $key, $message === null ) {
if ( is_null( $message ) ) {
$message = $key;
$key = null;
}
$this->messages['new'][$key] = $message;
$this->session->set('flash', $this->messages['new'] );
return true;
}
public function get( $item = null ) {
if( $item === null ) {
return $this->messages['current'];
}
if( ! array_key_exists($item, $this->messages['current']) ) {
return null;
}
return $this->messages['current'][$item];
}
}
Usage:
$flash = Flash::instance();
$flash->add('A random message');
$flash->add('some_key', 'Some message');
$flash->get(); // array( 0 => 'A random message', 'some_key' => 'Some message')
$flash->get('some_key'); // 'A Random message'
What it does basically is on initialization it retrieves the current message from the session, using the get_once() function. The variable is nou out of the Session object, so it will only last this request. Everytime you add a variable, it will immediately persisted to the Session object.
There is just one problem; if you are using ajax calls, the messages will only be available on the initial php request, not on subsequent ajax calls. And there is also no restriction whatsoever on what kind of variable you are storing (but it must be serializable). You'll have to build in some checks for that too.
warning: the class is not tested, so it would surprise me if you do not get a syntax error ;)
And to go a step further: you would need an extra refresh anyway. The request flow should be like this imo:
Request 1: User is presented form
Request 2: User posts the form, which is processed. Data is inserted in database. When done, user is redirected
Request 3: A confirmation page is shown (can be "thank you", or the detail page, whatever).
You would set the flash message in request 2, and show it in 3. I would not directly show the thank you page on request 2, because when the user refreshes, the form will be posted again.
Use this module. Works perfectly :)

Show latest posts from each custom post type with pre_get_posts

What I have
On my front page I have managed to alter the main query on the front page to show my custom post types with "pre_get_posts" like this:
function custom_post_types_in_home_loop( $query ) {
if ( ! is_admin() && is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'member', 'press', 'calendar_event' ) );
}
return $query;
}
add_filter( 'pre_get_posts', 'custom_post_types_in_home_loop' );
What I want
The problem is that some post types have way more posts than others, so I want to show only 3 posts per post type to get a little variation in the loop.
What I've tried
With some help from this and this answer ive managed to do this but nothing's happening, what am I doing wrong?
function custom_post_types_in_home_loop( $query ) {
if ( ! is_admin() && is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'member', 'press', 'calendar_event' ) );
if ( $query->get( 'post_type' ) == 'member' ) {
$query->set('posts_per_page', 3);
}
/* And the same 'if' statement for 'press' and 'calendar_event' */
}
return $query;
}
add_filter( 'pre_get_posts', 'custom_post_types_in_home_loop' );
You are setting post_type on the main query to an array. And then comparing it against a string. None of these conditionals are true and hence aren't executing.
If you simply want to limit the number of queries and pick a random order you can do so with,
$query->set('posts_per_page', 9);
$query->set('orderby', 'rand);
This will give you 9 posts, 3 each, randomly choosen between the different Custom Post Types.
To keep the different types together, you'll need to build a more complex and resource intensive groupby query. If you need this, I'd suggest building 3 different Custom Loops for each post_type instead.

CouchDB view to emit docs where doc element is a particular type

I'm new to couch, but I'm tracking down a bug and would like a view that will emit all docs of a certain type where the value of a certain key is not an array.
I tried
function(doc) {
if( doc.Type == "MyType" && !( doc.Stuff instanceof Array ) )
{
emit( doc._id, null );
}
}
But it's returning all the docs of type "MyType" even though doc.Stuff is an Array. Is what I want to do possible? Am I just making a stupid mistake?
For the record this is just a temporary view I'm using to hopefully track down a bug.
Try with:
if(doc.Type === "MyType" && !isArray(doc.Stuff))
You shouldn't rely on constructor or anything that relies on constructor like instanceof. I'd recommend duck typing this with something like this:
if( doc.Type == "MyType" && !( doc.Stuff.splice && doc.Stuff.join ) )

Resources